admin管理员组文章数量:1122832
I have a custom post type of "show" and within that custom post type I have set up a taxonomy / category called "venue" which has two options "venue-one" and "venue-two" (slugs). On the two archive pages I have set up, where each shows all posts from "venue-one" or "venue-two", pagination is not working past page 3. I am using numbered pagination and visually it does display what the correct number of pages should be (given what I set posts per page to) however if you click anything past "3" I am getting a 404.
What I have tried and has not worked:
- resetting permalinks
- changing max number of posts per page directly through wp dashboard
- getting rid of my custom query and using pre_get_posts (note: all my research points to this being the path to fixing my issue, so my assumption is it may work and I am just coding the function incorrectly in functions.php or otherwise). If using pre_get_posts in functions.php is actually the solution, do I need to add anything new in my template files of the problematic archive pages other than the standard wp loop? (and of course getting rid of the custom query).
Just to reiterate, everything works perfectly except for the fact pagination past page 3 gives a 404.
Last note, pagination does work perfectly on the archive page for this custom post as a whole. The issue only occurs on the archive pages for "venue-one" and "venue-two".
WP_Query
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$today = current_time('Ymd');
$args = array (
'post_type' => 'show',
'posts_per_page' => 3,
'paged' => $paged,
'meta_key' => 'show_date',
'order' => 'ASC',
'orderby' => 'meta_value',
'tax_query' => array(
array(
'taxonomy' => 'venue',
'field' => 'slug',
'terms' => 'venue-one'
)
),
'meta_query' => array(
array(
'key' => 'show_date',
'compare' => '>=',
'value' => $today
),
),
);
$the_query = new WP_Query($args);
?>
Loop
<?php if ($the_query->have_posts() ): while ($the_query->have_posts() ) :$the_query->the_post(); $fields = (object) get_fields(); ?>
Pagination function (in functions.php)
function pagination_bar_venue( $custom_query ) {
$total_pages = $custom_query->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,
));
}
}
Calling pagination in the template
<div class="pagination-shows">
<?php pagination_bar_venue($the_query); ?>
</div>
EDIT + SOME GOOD NEWS Got everything to work w/ the below pre_get_posts function but...the pagination links are not showing up under the posts now. To be clear, the below code has worked and I am now able to see posts beyond page 3 (up until whatever page has posts on it, all working perfect if I type in the url). However once implemented, the links to physically click on the page numbers are gone. How can I pass in $paged variable to the below so it shows back up? or is there another issue?
// get taxonomy posts
function get_tax_posts( $query ) {
// Make sure this only fires when we want it too
if( !is_admin() && $query->is_main_query() && $query->is_tax('venue')) {
// If so, modify the order variables
$query->set('post_type', 'show' );
$query->set('posts_per_page', '3' );
$query->set( 'meta_key', 'show_date' );
$query->set( 'order', 'ASC' );
$query->set( 'orderby', 'meta_value' );
$meta_query[] = array(
array(
'key' => 'show_date',
'value' => current_time('Ymd'),
'compare' => '>=',
),
);
$query->set('meta_query',array( $meta_query ) );
$taxquery = array(
array(
'taxonomy' => 'venue',
'field' => 'slug',
'terms' => 'venue-one',
)
);
$query->set( 'tax_query', $taxquery );
}
}
add_action('pre_get_posts', 'get_tax_posts', 9999);
Ok now pagination is fixed but noticed another issue. As for pagination, I swapped out the below:
<?php pagination_bar_venue($the_query);?>
With:
<?php the_posts_pagination();?>
and now pagination works as well.
New issue that is happening is based on the hook priority I believe. As noted above, I have (2) taxonomies to activate this for so I did 2 separate functions in functions php, one for venue-one (code above) and then another for venue-two (with a different function name of course). It seems like you can not have both working. The only one that will work is the one with the higher hook priority number. Is there a fix for this? thanks
EDIT - I think the issue was my function names were too similar, they were not exact but close outside of a few letters. I changed them to be nothing alike at all, and everything seems to be working now!
I have a custom post type of "show" and within that custom post type I have set up a taxonomy / category called "venue" which has two options "venue-one" and "venue-two" (slugs). On the two archive pages I have set up, where each shows all posts from "venue-one" or "venue-two", pagination is not working past page 3. I am using numbered pagination and visually it does display what the correct number of pages should be (given what I set posts per page to) however if you click anything past "3" I am getting a 404.
What I have tried and has not worked:
- resetting permalinks
- changing max number of posts per page directly through wp dashboard
- getting rid of my custom query and using pre_get_posts (note: all my research points to this being the path to fixing my issue, so my assumption is it may work and I am just coding the function incorrectly in functions.php or otherwise). If using pre_get_posts in functions.php is actually the solution, do I need to add anything new in my template files of the problematic archive pages other than the standard wp loop? (and of course getting rid of the custom query).
Just to reiterate, everything works perfectly except for the fact pagination past page 3 gives a 404.
Last note, pagination does work perfectly on the archive page for this custom post as a whole. The issue only occurs on the archive pages for "venue-one" and "venue-two".
WP_Query
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$today = current_time('Ymd');
$args = array (
'post_type' => 'show',
'posts_per_page' => 3,
'paged' => $paged,
'meta_key' => 'show_date',
'order' => 'ASC',
'orderby' => 'meta_value',
'tax_query' => array(
array(
'taxonomy' => 'venue',
'field' => 'slug',
'terms' => 'venue-one'
)
),
'meta_query' => array(
array(
'key' => 'show_date',
'compare' => '>=',
'value' => $today
),
),
);
$the_query = new WP_Query($args);
?>
Loop
<?php if ($the_query->have_posts() ): while ($the_query->have_posts() ) :$the_query->the_post(); $fields = (object) get_fields(); ?>
Pagination function (in functions.php)
function pagination_bar_venue( $custom_query ) {
$total_pages = $custom_query->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,
));
}
}
Calling pagination in the template
<div class="pagination-shows">
<?php pagination_bar_venue($the_query); ?>
</div>
EDIT + SOME GOOD NEWS Got everything to work w/ the below pre_get_posts function but...the pagination links are not showing up under the posts now. To be clear, the below code has worked and I am now able to see posts beyond page 3 (up until whatever page has posts on it, all working perfect if I type in the url). However once implemented, the links to physically click on the page numbers are gone. How can I pass in $paged variable to the below so it shows back up? or is there another issue?
// get taxonomy posts
function get_tax_posts( $query ) {
// Make sure this only fires when we want it too
if( !is_admin() && $query->is_main_query() && $query->is_tax('venue')) {
// If so, modify the order variables
$query->set('post_type', 'show' );
$query->set('posts_per_page', '3' );
$query->set( 'meta_key', 'show_date' );
$query->set( 'order', 'ASC' );
$query->set( 'orderby', 'meta_value' );
$meta_query[] = array(
array(
'key' => 'show_date',
'value' => current_time('Ymd'),
'compare' => '>=',
),
);
$query->set('meta_query',array( $meta_query ) );
$taxquery = array(
array(
'taxonomy' => 'venue',
'field' => 'slug',
'terms' => 'venue-one',
)
);
$query->set( 'tax_query', $taxquery );
}
}
add_action('pre_get_posts', 'get_tax_posts', 9999);
Ok now pagination is fixed but noticed another issue. As for pagination, I swapped out the below:
<?php pagination_bar_venue($the_query);?>
With:
<?php the_posts_pagination();?>
and now pagination works as well.
New issue that is happening is based on the hook priority I believe. As noted above, I have (2) taxonomies to activate this for so I did 2 separate functions in functions php, one for venue-one (code above) and then another for venue-two (with a different function name of course). It seems like you can not have both working. The only one that will work is the one with the higher hook priority number. Is there a fix for this? thanks
EDIT - I think the issue was my function names were too similar, they were not exact but close outside of a few letters. I changed them to be nothing alike at all, and everything seems to be working now!
Share Improve this question edited Jan 24, 2019 at 7:25 James asked Jan 23, 2019 at 16:21 JamesJames 211 silver badge9 bronze badges 10 | Show 5 more comments1 Answer
Reset to default 0I had a similar issue and maybe this is also helpful when pagination is only working sometimes: e.g. /page/1/ and /page/2/ are working and /page/3/ not. Result: Error 404. Problem is: default value (12 items each page) for posts_per_page (WordPress Settings/Reading) is loaded always from database before the template is loaded. So $args=['posts_per_page' => 1] in custom_post WP_Query($args) has effect on pagination and number of loaded elements but not on paged rewrite Rule! Proposed solution is to add pre_get_posts() to function.php and separate by custom post_type. Example:
//Add to functions.php because it have to be loaded before template loading
function hwl_home_pagesize( $query ) {
if ( get_post_type() == "your_custom_post_type" )
$query->query_vars['posts_per_page'] = 2; //same as in WP_Query arg or add to WP_Query arg get_option('posts_per_page')
}
add_action('pre_get_posts', 'hwl_home_pagesize', 1);
see also: https://core.trac.wordpress.org/ticket/22299 (From my point of view, it is still an architecture issue, no matter what was already said 9years ago.)
本文标签: pre get postsPagination not working past page 3 on archive page of category
版权声明:本文标题:pre get posts - Pagination not working past page 3 on archive page of category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736292729a1928995.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var_dump($wp_query)
after the query is run, you can see how many pages the main query has in both cases. Usepre_get_posts
to apply your modifications to the main query. – Milo Commented Jan 23, 2019 at 17:03