未找到自定义分页 404 的自定义分类法和自定义帖子类型

2022-01-04 00:00:00 php wordpress pagination

当来自 General -> Reading 的帖子小于我的自定义分类法 cities(自定义帖子类型 city).从我在 SO 上看到的情况来看,这个问题通常可以通过使用 pre_get_posts 过滤器来解决,但不知道如何将它应用于我的案例.我想提一下,在 taxonomy-cities.php 中,我收到了自定义帖子类型的自定义分类法类别的帖子.

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

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();

对于archive-city.php 我在functions.php 中使用这个过滤器并且它工作正常,但它没有被应用到taxonomy-cities.php 所以我得到 404:

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' );

推荐答案

这是一个老问题,但我最近遇到了同样的问题.以下代码段实际上是我的 taxonomy-template.php 使用标准 WP_Query,示例此处.

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.

如何正确分页自定义帖子类型分类

查询

  • 首先因为它是一个分类模板,所以第一行声明了 $term 允许我访问术语数组,因此可以访问分类 id、名称 url 等.

    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.

      其次我将术语 id 设置为要在 tax_query 参数.这用于查询我的自定义帖子类型 videos,然后获取循环的分类(类别),存储在 $term_id 中.

      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.

      • 因为我循环浏览帖子并限制结果,我们实际上需要搜索我们剩余的帖子,同时排除之前的结果和之后的结果.这意味着很简单:
      • 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:

      重要:

      'exclude_from_search' =>假,

      <?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; ?>
      

相关文章