Custom query give me 404 when paginate
in my site I have "CPT Clients" and each client has "CPT Success Stories", also I have a list of client’s taxonomies, and when access to one of thees, I need to display the stories which client belongs to that taxonomy.
My code looks like this:
$query_obj = get_queried_object();
$args = array(
'post_type' => 'sky-client',
'posts_per_page' => -1,
'meta_key' => 'sky_client_featured_success_story',
'tax_query' => array(
array(
'taxonomy' => $query_obj->taxonomy,
'field' => 'id',
'terms' => array( $query_obj->term_taxonomy_id ),
),
),
);
$stories = array();
$clients = get_posts( $args );
foreach ( $clients as $client ) :
array_push( $stories, get_post_meta( $client->ID, 'sky_client_featured_success_story', true ) );
endforeach;
$args = array(
'post_type' => 'sky-success-story',
'posts_per_page' => 1,
'post__in' => $stories,
'paged' => get_query_var( 'paged' ),
);
$the_query = new WP_Query ( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) :
$the_query->the_post();
get_template_part( '/template-parts/content', 'sky-success-story' );
endwhile;
endif;
It works fine, but when I paging it sends me a 404 error. So far I’ve tried many ways like using the taxonomy-sky-client-category.php
file, pre_get_posts
action hook and finally a custom template like this:
function sky_success_stories_archive(){
if ( ! is_taxonomy( 'sky-client-category' ) )
return;
return locate_template( 'success-stories-client-category-archive.php' );
}
add_action( 'taxonomy_template', 'sky_success_stories_archive' );
Note that using pre_get_posts
not even display any thing in the page:
function sky_tax_client_category_filter( $query ){
if ( ! is_admin() && $query->is_main_query() && ! $query->is_search ) :
$query_obj = get_queried_object();
if ( $query->is_tax( 'sky-client-category', $query_obj->term_taxonomy_id ) ) :
$args = array(
'post_type' => 'sky-client',
'posts_per_page' => -1,
'meta_key' => 'sky_client_featured_success_story',
'tax_query' => array(
array(
'taxonomy' => $query_obj->taxonomy,
'field' => 'slug',
'terms' => $query_obj->slug,
),
),
);
$stories = array();
$clients = get_posts( $args );
foreach ( $clients as $client ) :
array_push( $stories, get_post_meta( $client->ID, 'sky_client_featured_success_story', true ) );
endforeach;
$query->set( 'post_type', 'sky-success-story' );
$query->set( 'posts_per_page', 1 );
$query->set( 'post__in', $stories );
$query->set( 'paged', get_query_var( 'paged' ) );
endif;
endif;
}
add_action( 'pre_get_posts', 'sky_tax_client_category_filter' );
That is, the use of both taxonomy-sky-client-category.php
and custom template works, but gives me a 404 when paging.
Thanks in advance for any help
Leave an answer
You must login or register to add a new answer .