Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

php - Custom taxonomy and custom post type with custom pagination 404 not found

My pagination is throwing a 404 error when the posts from General -> Reading are smaller than my custom number of posts on my custom taxonomy cities (custom post type city). From what I saw on SO this issue can be fixed usually by using a filter of pre_get_posts, but don't know how to apply it on my case. I want to mention that in the taxonomy-cities.php I am getting posts of a category of a custom taxonomy of a custom post type.

taxonomy-cities.php

$cat_ID = get_query_var('cities');
$custom_id = get_term_by('slug', $cat_ID, 'cities');
add_filter('posts_orderby', 'edit_posts_orderby');
function edit_posts_orderby($orderby_statement) {
    global $aPostsIDs;
    $orderby_statement = 'FIELD(ID, '.implode(',',$_SESSION['saved_city_ids']).')';
    return $orderby_statement;
}
$offset     = ($paged - 1) * $num_city_guides_post; 
$args['post_type'] = 'city'; 
$args['posts_per_page'] = $num_city_guides_post; // 5
$args['orderby'] = $orderby; // rand
$args['order'] = $order; // DESC
$args['paged'] = $paged; // 1
$args['tax_query'] = array(
                        array(
                               'taxonomy' => 'cities',
                               'field' => 'id',
                               'terms' =>  array($custom_id->term_id) // 3742
                             )
                    );
}
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) : 
  while ( $wp_query->have_posts() ) : $wp_query->the_post();
  // some code here
  endwhile; 
else: echo 'No Posts';
endif; 
wp_reset_postdata();

For archive-city.php I am using this filter in the functions.php and it works fine, but it doesn't get applied to the taxonomy-cities.php so I get 404:

function portfolio_posts_per_page_city( $query ) {
        if (array_key_exists('post_type', $query->query_vars)) {
        if ( $query->query_vars['post_type'] == 'city' ) 
        $num_city_guides_post = get_option('num_city_post');
        if( empty( $num_city_guides_post ) )
        $num_city_guides_post = 9;

        $query->query_vars['posts_per_page'] = $num_city_guides_post;
        return $query;
    }
}
if ( !is_admin() ) add_filter( 'pre_get_posts', 'portfolio_posts_per_page_city' );
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is an old question however i recently had the same issue. The following Snippet is in fact the contents of my taxonomy-template.php using the standard WP_Query, example here.

How to properly paginate custom post type taxonomy

The Query

  • Firstly because it's a taxonomy template, the first line declares $term allowing me to access the term array and therefore the taxonomy id, name url etc.

  • Secondly i'm setting the term id as a variable to be used in the tax_query parameter. This is used to query my custom post type videos and then get the taxonomy (category) for the loop, stored in $term_id.

The Fix

  • Because i'm looping through the posts and limiting the results, we effectively need to search for our remaining posts while excluding the previous results and results thereafter. What this means is quite simple:

You must make your custom post types searchable in order for pagination to work:

Important:

'exclude_from_search' => false,


Example taxonomy-template.php

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>

<?php
$term_id = $term->term_id;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
  'post_type'       => 'videos',
  'posts_per_page'  => 4,
  'paged'           => $paged,
  'tax_query'       => array(
    array(
      'taxonomy' => $term->taxonomy,
      'field' => 'term_id',
      'terms' => $term_id
    )
  )
);
$the_query = new WP_Query( $args );
?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  <!-- Loop -->
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) :  ?>
<?php endif; ?>
<?php else: ?>
  <!-- Nothing Found -->
<?php endif; ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...