I am trying to get working pagination in my WP_Query.
After trying more then 2 hour, no way except stackoverflow :-D.
What is my problem
Older and newer pagination links are appreaing and when I click on them, then it take me to the correct url which is : /?paged=2.
But post list did not change, same post on every page.
Here is my code
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'cat' => $cat,
(($paged != '') ? 'paged =>'. $paged : ''),
'posts_per_page' => $post_to_show
);
print_r($args);
$the_query = new WP_Query($args);
while ($the_query->have_posts()) : $the_query->the_post();
//post template
endwhile;
if ( get_next_posts_link() || get_previous_posts_link() ) {
echo '<div class="wp-navigation clearfix">
<div class="alignleft">'.next_posts_link('« Older Entries').'</div>
<div class="alignright">'.previous_posts_link('Newer Entries »').'</div>
</div>';
}
wp_reset_query();
Your $args array looks wrong. Also, $paged will never be empty (because it always gets assigned the default value of 1), so your check is redundant.
There is nothing wrong with passing 1 as page number.
$args = array(
'cat' => $cat,
'paged' => $paged,
'posts_per_page' => $post_to_show
);
Related
How to add pagination for comments in page with custom code in Wordpress?
My code:
<?php
<ol class="comment-list">
<?php
$comments = get_comments(array(
'number' => '8',
'status' => 'approve',
'offset' => $offset
));
foreach( $comments as $comment ){
echo $comment->comment_author . '<br />' . $comment->comment_content;
}
?>
</ol>
<?php paginate_comments_links ( array( 'prev_text' => '« PREV' , 'next_text' => 'NEXT »' ) ); ?>
I searched here for solutions, but did not find any. There are old posts from before 2014. Other codes given here do not work. I have been struggling for several hours.
Please help.
How are you setting the $offset variable? Try something like this, code is not tested but it will give you idea to get comments pagination working.
<?php
$current_page = get_query_var( 'cpage' ) ? get_query_var( 'cpage' ) : 1;
$comments_per_page = 8;
$offset = ( $current_page - 1 ) * $comments_per_page;
I have an issue trying to get the current page from WP pagination. Here is my code:
global $wp_query;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
var_dump(get_query_var( 'paged' )); // HERE RETURN 0 EVERYTIME
$args = [
'post_type' => 'post',
'posts_per_page' => 30,
'paged' => $paged
];
$loop = new WP_Query($args);
while ($loop->have_posts()) :
$loop->the_post(); ?>
// code ....
<?php endwhile; ?>
I use this code on a single-page template (and so from a single-page URL). Doing get_query_var( 'paged' ); returns 0.
How to get the current page from a single-page template?
As per documentation:
For getting the current pagination number on a static front page (Page
template) you have to use the 'page' query variable:
<?php $page = (int)get_query_var( 'page', 1 ); ?>
<h1>Currently Browsing Page <?php echo $page; ?> On a static front page</h1>
I have a search form. Choose a taxonomy, and select one or more terms. I want to paginate the results.
Here is the code:
<?php
if(isset($_POST['tax_type'])) {
$tax_type=$_POST['tax_type'];
$terms = $_POST[$tax_type];
$term_list = implode("','", $terms);
$term_list1 = implode(", ", $terms);
$term_list = "'".$term_list."'";
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$giftfinder_args = array(
'post_type' => 'products',
'posts_per_page'=>"1",
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => $tax_type,
'field' => 'slug',
'terms' => explode(',',$term_list),
),
),
);
echo '<div class="results"><span class="eyebrow">search results</span>';
echo '<div class="cat_label">“' . $term_list1 . '”</div></div>';
// the query
$giftfinder_query = new WP_Query( $giftfinder_args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $giftfinder_query;
?>
<?php if ( $giftfinder_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $giftfinder_query->have_posts() ) : $giftfinder_query->the_post();
$thumb = get_the_post_thumbnail(null, 'medium');
?>
<div class="prod"><?php if(empty($thumb)) {echo '<div style="width:265px;height:265px;background:#eee;"></div>';} else {the_post_thumbnail('medium');} ?><span class="truncate"><?php the_title(); ?></span></div>
<?php endwhile; ?>
<div style="clear:both;"></div>
<!-- end of the loop -->
<?php
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'twentyfifteen' ),
'next_text' => __( 'Next page', 'twentyfifteen' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
) );
?>
<?php wp_reset_postdata(); ?>
<?php else : get_template_part( 'content', 'none' ); endif; ?>
<?php } ?>
First page is fine, but no results appear on paginated pages.
As you can see, I'm trying to use the pagination fix that turns my query into $wp_query:
// the query
$the_query = new WP_Query( $args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
But like I said, no results beyond first page.
I was going to try pre_get_posts but I can't figure out how to code it since every query has unique arguments based on the user's choice of taxonomy and searched terms.
I also tried 'add_args' to the paginate_links() function but could not figure out how to format the resulting get string.
BTW, right now, the query just returns one post to make it easy to check pagination.
Any suggestions appreciated!
Not sure if this is the only issue, but it looks like you have a typo.
You are setting $page:
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
But looking for $paged:
'paged' => $paged,
I have two custom post types for my site, but am using the same Categories/Tags for both.
I am trying to create a category.php page that will allow me to show all items within that category, but in two separate areas, one for each type.
I can get the first post type to display in the main loop, but how can I structure a second loop to display only the posts in the second type that are of that category?
I suggest you use to instances of WP_Query and in args section, for both sections have set post_type= for the desired one.
<?php
//general code to get the current category id
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'cat' => $cat_id,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo get_the_title();
echo get_the_excerpt();
endwhile;
wp_reset_postdata();
//for second loop of second post type
$args1 = array(
'post_type' => 'your_post_type',
'posts_per_page' => 5,
'cat' => $cat_id,
);
$the_query1 = new WP_Query( $args1 );
if ( $the_query1->have_posts() ) :
while ( $the_query1->have_posts() ) : $the_query1->the_post();
echo get_the_title();
echo get_the_excerpt();
endwhile;
wp_reset_postdata();
I'm new to loop customisation and I have an issue getting my search page front end filter loop working in the same way as my other category template post loop.
This is the loop I am using for the category templates which works fine.
<?php $custom_query_args = array( 'cat' => '2', 'posts_per_page' => 6 );
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$custom_query = new WP_Query( $custom_query_args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post(); ?>
This allows me to paginate through all posts in this category but I need to convert a similar code block on my search page to allow it to do the same, currently the search template code below doesn't paginate like the above.
<?php wp_reset_postdata();
global $wp_query;
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
foreach($_POST['filter_cat'] as $fcat){$fcat_list .= $fcat . ",";}
query_posts(
array_merge(
array(
'cat' => $fcat_list,
'posts_per_page' => 6,
'paged' => $paged
),
$wp_query->query
)
); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I know that the first code block is a better method but I can't figure out how to use the second block in the same way.
Edit:
Ok please excuse my inexperience.
Thanks to Pieter Goosen I'm part way there with using pre_get_posts.
I've done so successfully with category template but using the following for the search page template kills the site, I'm not sure where I'm going wrong.
function paginate_search ( $query ) {
if( $query->!is_admin() ) {
foreach($_POST['filter_cat'] as $fcat){$fcat_list .= $fcat . ",";}
query_posts( array_merge ( array (
'cat' => $fcat_list,
'posts_per_page' => 6,
'paged' => $paged
),
$wp_query->query
) ); } }
add_action( 'pre_get_posts', 'paginate_search' );