wp_query same data on every page - arrays

In my wordpress site where I use ACF, I created an array of arrays with wp_query and imported them into the datatable. It also acts according to the posts_per_page wordpress settings. Currently wordpress is set to 10 impressions per page. I also wrote code in function.php to display the page under the table. 10 data comes into the table. Below is the correct number of pages. The problem starts here. Although the number of pages is correct, I always see the same 10 data when I click on the pages. Where did I make the mistake?
<?php
$show_logo = (!empty($date_or_logo) and $date_or_logo == 'logo') ? true : false;
$posts_per_page = (!empty($number) and intval($number)) ? $number : get_option( 'posts_per_page' );
$current = (!empty($offset) and intval($offset)) ? $offset : 1;
$offset = (!empty($offset) and intval($offset)) ? ( $offset * $posts_per_page ) - $posts_per_page : 0;
$args = array(
'post_type' => 'stm_ico_listing',
'posts_per_page' => $posts_per_page,
'offset' => $offset,
'meta_key' => 'end_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'end_date',
'compare' => '<',
'value' => time(),
),
array(
'key' => 'end_date',
'compare' => '!=',
'value' => '',
),
)
);
$args = (!empty($add_args)) ? wp_parse_args($add_args, $args) : $args;
$q = new WP_Query($args);
if ($q->have_posts()): ?>
<!-- the loop -->
<?php while ($q->have_posts()): $q->the_post();
?>
<tr>
<td width="10%">
.............
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<!-- end of the loop -->
<ul class='page-numbers'>
<li><?php pagination_bar_venue($q); ?></li>
</ul>
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
<script>
$(document).ready(function() {
$('#table').DataTable( {
"language": {
"lengthMenu": "Display _MENU_ records. per page",
"zeroRecords": "Nothing found - sorry",
"info": "Showing page _PAGE_ of _PAGES_",
"infoEmpty": "No records available",
"infoFiltered": "(filtered from _MAX_ total records)"
},
"lengthMenu": [[50, 100, -1], [50, 100, "All"]]
} );
} );
</script>
function.php
add_action( 'wp_enqueue_scripts', 'team_script' );
function pagination_bar_venue( $q ) {
$total_pages = $q->max_num_pages;
$big = 999999999; // need an unlikely integer
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total_pages,
));
}
}

Related

How to do multiple pagination without page not found error in Cakephp

I have paginate 2 models (Income,Expanse) in the Student model view page .
I have really page not found problem with cakephp's pagination.
It have no any problem when the paginate result has only 1 page or the same, but It will error if one has more paginate result than other.
for example.
-income has paginate result 1 page and expanse has 1 page. No problem at all.
-income has 10 pages and expanse has 10 pages no problem at all.
-income has 2 pages and expanse has 1 page. income page 2 page not found error.
-income has 5 pages and expanse has 2 pages. income page 3,4,5 page not found error.
-income has 10 pages and expanse has 13 pages. expanse page 11,12,13 page not found error.
for example(not real one) , Student's view have income and expense items ,Both are display as pagination.
//this is how I config paginator in Student controller.
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'recursive'=>0
),
'Expense' => array (
'order'=>array('expense_id'=>'ASC'),
'limit'=>10,
'recursive'=>0,
)
);
<div class="paging">//this is how I config Income paginator in Student view
<?php
echo $this->Paginator->prev('< ' . __('previous'), array('model'=>'Income'), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('model'=>'Income','separator' => '','modulus'=>100,'first'=>'หน้าแรก','last'=>'หน้าสุดท้าย'));
echo $this->Paginator->next(__('next') . ' >', array('model'=>'Income'), null, array('class' => 'next disabled'));
?>
</div>
//this is how I config Expanse paginator in Student view
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array('model'=>'Expanse'), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('model'=>'Expanse','separator' => '','modulus'=>100,'first'=>'หน้าแรก','last'=>'หน้าสุดท้าย'));
echo $this->Paginator->next(__('next') . ' >', array('model'=>'Expanse'), null, array('class' => 'next disabled'));
?>
</div>
Please help me. sorry for my english
If you have any question , please ask me.
Thank you.
Yes It is possible.....
Step1 : Make paging.ctp in View/Elements/paging.ctp
<?php if (!isset($this->Paginator->params['paging'])) {
return;
}
if (!isset($model) || $this->Paginator->params['paging'][$model]['pageCount'] < 2) {
return;
}
if (!isset($options)) {
$options = array();
}
$options['model'] = $model;
$options['url']['model'] = $model;
$this->Paginator->__defaultModel = $model;
?>
<div class="paging">
<ul class="pagination pull-right">
<?php
echo $this->Paginator->prev('<<', array_merge(array('class' => '', 'tag' => 'li'), $options), null, array('class' => 'disabled', 'tag' => 'li'));
echo $this->Paginator->numbers(am($options,array('tag' => 'li', 'separator' => '', 'currentClass' => 'active', 'currentTag' => 'a')));
echo $this->Paginator->next('>>', array_merge(array('class' => '', 'tag' => 'li'), $options), null, array('class' => 'disabled', 'tag' => 'li'));
?>
</ul>
</div>
Step2: Now add this function to Controllers/AppController
public function pageForPagination($model) {
$page = 1;
$sameModel = isset($this->request->params['named']['model']) && $this->request->params['named']['model'] == $model;
$pageInUrl = isset($this->request->params['named']['page']);
if ($sameModel && $pageInUrl) {
$page = $this->request->params['named']['page'];
}
$this->passedArgs['page'] = $page;
return $page;
}
Step3: Now in controller action do some conditions so that proper page should be call
if (empty($this->request->params['named'])) {
$page = $this->pageForPagination('Income');
$page1 = $this->pageForPagination('Expense');
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page,
'recursive'=>0
),
'Expense' => array (
'order'=>array('expense_id'=>'ASC'),
'limit'=>10,
'page' => $page1,
'recursive'=>0,
));
} else if ($this->request->params['named']['model'] == 'Income') {
$page = $this->pageForPagination('Income');
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page,
'recursive'=>0
));
} else if ($this->request->params['named']['model'] == 'Expense') {
$page1 = $this->pageForPagination('Expense');
public $paginate = array(
'Expense' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page1,
'recursive'=>0
));
Step 4: Now call the paging element in your student view
<?php echo $this->element('paging', array('model' => 'Income'));?>
<?php echo $this->element('paging', array('model' => 'Expense'));?>
Note: Please take care of brackets and semicolon.......and sorry to be late but will help others...Thanks..
I think if you use two different iframes to load different URLs for pagination, you can handle this.
OR you have to call ajax functions to paginate 2 models in one page without reloading the page....

How cakePHP build form data

i've created a form that loop in my table and give me some images:
id1: ->Image path, title etc
id2: -> Image path title etc
i've created my view in this way:
<div>
<?php echo $this->Form->create('Gallery',
array( 'action' => 'save')); ?>
<table cellpadding="0" cellspacing="0">
<?php foreach($images as $image): ?>
<tr>
<td>
<?php echo $this->PhpThumb->thumbnail($image['medias']['file'], array('w' => 75, 'h' => 75)); ?>
</td>
<td><?php echo $this->Form->input('title'.$image['medias']['id'], array('value' => $image['medias']['title']));?></td>
<td><?php echo $this->Form->input('description'.$image['medias']['id'], array('value' => $image['medias']['description']));?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $this->Form->end(__('Submit')); ?>
After submit, i have an array like this:
array(
'Gallery' => array(
'title19' => 'test',
'description19' => '',
'title20' => '',
'description20' => '',
'title21' => '',
'description21' => '',
'title22' => '',
'description22' => ''
)
)
For sure, i cant use the save() method because the array isnt well formatted. I think the right format should be:
array(
'Gallery' => array(
array('id' => 19,
'title => 'test',
'description => ''),
array( 'id' =>20,
title => '',
description => '')
)
)
but, how can i get this ? Or some array that is right formatted?
When you iterate over them put the iterator or id in the right place:
$this->Form->input('Model.' . $i . 'field');
$this->Form->input('Model.' . $i . 'other_field');
References:
Saving your data
Form Helper

Wordpress: Continues(loop) page navigation with get_permalink()

I would like to turn my page navigation in to a loop. I'm quit close but my problem is that I'm not able to switch between the categories. At the moment I have a loop inside of the category but I'm not able to jump to the next category.
As information: I have different post categories.
My script:
<?php
$prev_post = get_adjacent_post( true, '', true );
$next_post = get_adjacent_post( true, '', false );
$prev_post_id = $prev_post->ID;
$next_post_id = $next_post->ID;
?>
<?php
if(($prev_post_id === '') || ($prev_post_id === null)) :
$query = new WP_Query(array('order' => 'DESC', 'posts_per_page' => '1', 'cat=4'));
if($query->have_posts()) : while($query->have_posts()) :
$query->the_post();
$prev_post_id = get_the_ID();
endwhile;
endif;
wp_reset_postdata();
endif;
if(($next_post_id === '') || ($next_post_id === null) ) :
$query = new WP_Query(array('order' => 'ASC', 'posts_per_page' => '1', 'cat=4'));
if($query->have_posts()) : while($query->have_posts()) :
$query->the_post();
$next_post_id = get_the_ID();
endwhile;
endif;
wp_reset_postdata();
endif;
?>
<div id="left">
</div>
<div id="right">
</div>
$query = new WP_Query(array('order' => 'ASC', 'posts_per_page' => '1', 'cat=4'));
I tried different usage with the cat=4 or cat=1,2,3,4 but it has no effect.

cakePHP Paginator issue with second model

still a novice, but I am fighting with the paginator to limit my view to 10 records [properties] and to sort it on a field in the properties model. What am I doing wrong or miss in my code to get the paginator to control my view???
I have 2 models, Regions and Properties, a region has many properties, for example, my view is /region/view/13 This shows all properties for region 13.
The paginator is displaying the correct amount of properties, set the pages correct and all seems correct, but the paginator is not limiting my view, it just displays all properties by that region in one huge list instead of limit the view to 10 per page.
The sort per sleeps doesn't work either although the url in the browser seems to be ok.
/regions/view/13/sort:sleeps/direction:asc
/regions/view/13/sort:sleeps/direction:desc
Model:
<?php
App::uses('AppModel', 'Model');
/**
* Region Model
*
* #property Country $Country
* #property Property $Property
*/
class Region extends AppModel {
/**
* Display field
*
* #var string
*/
public $displayField = 'regionname';
/**
* belongsTo associations
*
* #var array
*/
public $belongsTo = array(
'Country' => array(
'className' => 'Country',
'foreignKey' => 'country_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* #var array
*/
public $hasMany = array(
'Property' => array(
'className' => 'Property',
'foreignKey' => 'region_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
Controller:
public function view($id = null) {
if (!$this->Region->exists($id)) {
throw new NotFoundException(__('Invalid region'));
}
$this->Region->recursive = 2; // related to associated model data, Region -> Properties -> PropertyImages
$options = array('conditions' => array('Region.' . $this->Region->primaryKey => $id));
$total = $this->Region->Property->find('count', array(
'conditions' => array('Property.region_id' => $id)
));
$this->set('total', $total); // Push to counter the view.
$this->set('region', $this->Region->find('first', $options)); // Push the properties to the view.
$this->paginate = array(
'limit' => 10,
'conditions' => array('Property.region_id' => $id),
);
$this->Region->Property->virtualFields['sleeps'] = 'Property.sleeps';
$this->set('regions', $this->paginate('Property'));
}
View:
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled')); ?>
</div>
<br>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}'))); ?><br>
Total properties: <?php echo $total . "\n"; ?><br>
Sort by: <?php echo $this->Paginator->sort('sleeps'); ?>
<?php if (!empty($region['Property'])): ?>
<div class="regionviewfooter"></div>
<?php
$i = 0;
foreach ($region['Property'] as $property): ?>
<!-- We only need 1 image to show up -->
<?php foreach ($property['PropertyImage'] as $key =>$propertyImage): ?>
<div class="regionview">
<div class="regionviewleft">
<?php echo '<span style="font-size:12px;line-height:18px;font-weight:bold;">'; echo $this->Html->link(__($property['description']), array('controller' => 'properties', 'action' => 'view', $property['id'])); echo '</span>'; ?>
<div class="regionviewheader">Sleeps <?php echo $property['sleeps']; echo ' :: ' ?>
<?php
if ($property['minPrice'] == 0) {
echo 'Please call for prices';
} else {
echo 'Prices from ';
echo $this->Number->currency($property['minPrice'], 'GBP');
if ($property['maxPrice'] == 0) {
echo ' PW';
} else {
echo ' - ';
echo $this->Number->currency($property['maxPrice'], 'GBP');
echo ' PW';
}
}
echo '<span style="font-size:11px;line-height:18px;font-weight:normal;">'; echo ' :: ';
echo $this->Html->link(__('View Property'), array('controller' => 'properties', 'action' => 'view', $property['id'])); echo '</span>'; ?>
</div>
<?php echo ($property['shortDesc']); ?><br>
Add to my Enquiry List :: Property Ref. (<?php echo strtoupper($property['ref']); ?>)
</div>
<div class="regionviewright">
<!-- display image -->
<?php echo $this->Html->image(($propertyImage['filename']), array('alt' => ($propertyImage['description']),'width' => '150' ,'height' => '77')); $key ?>
<?php $key++; ?>
</div>
</div>
<div class="clear"></div>
<div class="regionviewfooter"></div>
<?php if ($key == 1) {
break; // Stop, we did 1 image.
}
?>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif; ?>
<br>Top of Page<br><br>
You're setting your paginated results to $regions, yet in your view, you're looping through and displaying $region (singular), which is a straight-up find() above, with recursive 2, which will pull all properties.
So - although there are many other things that need cleaned up with your code, for now, just use $regions (plural) instead of $region singular in your View.

Wordpress query_post array with Advanced Custom Fields (probably a simple solution)

Working on a Wordpress site and was wondering if anyone could point me in the right direction.
I have the following query_post which filters posts on my template and its working great.
query_posts( array( 'category_name' => 'galoretv', 'post_type' => 'post', 'paged'=>$paged, 'showposts'=>0, ) );
How would I append this to include a check for a specific value from Advanced Custom Fields?
Posts in this category have a radio button with three options 'primary' 'secondary' 'standard'
I want to be able to check against each value i.e if you belong in 'galoretv' and 'standard' do this.
I have the page executing and sorting with the parameters above, just not sure how to add the ACF value. I was able to get it to work using the sticky option but thats gonna work cause i need to have primary and secondar optionality. This is show i had it working with the sticky.
query_posts( array( 'category_name' => 'galoretv', 'post_type' => 'post', 'paged'=>$paged, 'showposts'=>0, 'post__in'=>get_option('sticky_posts')) );
The radio buttons field is called 'landing-grid-placement'
Any thoughts? Been checking the documentation, can't figure it out.
http://www.advancedcustomfields.com/docs/field-types/checkbox/
Thought this would work but it didn't
query_posts( array( 'category_name' => 'galoretv', 'post_type' => 'post', 'paged'=>$paged, 'showposts'=>0, 'landing-grid-placement' => 'primary') );
Any help would be greatly appreciated. This is probably a simple syntax issue but its eluding me and causing me a lot of issues. Been searching for an answer but haven't got it right yet. Thank you advance to whoever reads this and a double thank you to anyone who contributes a solution.
APPENDED CODE PER NOTE BELOW
<?php
$args = array(
'post_type' => 'post',
'category-slug' => 'models-galore',
'showposts'=>1,
'meta_query' => array(
array(
'key' => 'grid_location',
'value' => 'primary',
'compare' => '=',
'type' => 'CHAR'
)
)
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
?>
<li class="span8">
<div class="thumbnail">
<?php echo get_the_post_thumbnail( get_the_ID(), 'media-large-thumb' ); ?>
<h3>
<?php the_title(); ?>
</h3>
</div>
</li>
<?php
}
}
?>
You can do this with a meta_query
Here's the doc
And here's an example:
$args = array(
//...
'post_type' => 'post',
'paged'=>$paged,
//...
'meta_query' => array(
array(
'key' => 'landing-grid-placement',
'value' => 'primary',
'compare' => '=', //default
'type' => 'CHAR' //default
)
)
);
//This is the shoposts option (deprecated since 2.1, now use posts_per_page)
$args['posts_per_page'] = -1; //-1 = infinite
//to add taxonomy
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'galoretv'
)
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
?>
<li class="span8">
<div class="thumbnail">
<?php echo get_the_post_thumbnail( get_the_ID(), 'media-large-thumb' ); ?>
<h2>
<?php the_title(); ?>
</h2>
</div>
</li>
<?php
}
}
Maybe there is a simpler solution for you :
$args = array(
//...
'post_type' => 'post',
'paged'=>$paged,
//...
'meta_key' => 'landing-grid-placement',
'meta_value' => 'primary',
'meta_compare' => '=' //default
)
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
//do what you would normally do in your loop
}
}

Resources