dtziv24262 2016-11-22 22:29
浏览 35
已采纳

WP与query和post_type冲突

I am having a conflicting issue with a custom post type. On my archive pages, the custom post type was not showing up with the general posts, so I had to add in this pre_get_posts filter:

add_action( 'init', 'editions' );
function editions() {
  $labels = array(
  'name'               => _x( 'Editions', 'post type general name' ),
  'singular_name'      => _x( 'Edtiion', 'post type singular name' ),
  'add_new'            => _x( 'Add New', 'book' ),
  'add_new_item'       => __( 'Add New Edition' ),
  'edit_item'          => __( 'Edit Edition' ),
  'new_item'           => __( 'New Edition' ),
  'all_items'          => __( 'All Editions' ),
  'view_item'          => __( 'View Edition' ),
  'search_items'       => __( 'Search Editions' ),
  'not_found'          => __( 'No editions found' ),
  'not_found_in_trash' => __( 'No editions found in the Trash' ), 
  'parent_item_colon'  => '',
  'menu_name'          => 'Editions',
);
$args = array(
  'labels'        => $labels,
  'description'   => 'Editions and edition specific data',
  'menu_icon'     => 'dashicons-building',
  'public'        => true,
  'menu_position' => 5,
  'supports'      => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt' ),
  'taxonomies'    => array( 'edition', 'post_tag'),
  'has_archive'   => true,
);
register_post_type( 'edition', $args ); 
}

add_action( 'init', 'register_taxonomy_for_edition' );
function register_taxonomy_for_edition() {
  register_taxonomy_for_object_type( 'post_tag', 'edition' );
};
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
    if( !is_admin() ) {
        if ( !is_post_type_archive() && $query->is_archive() ) {
            $query->set( 'post_type', array( 'post', 'edition' ) );
        }
    return $query;
    }
}

This does what I want it to do, and in the archive page shows the general posts, and a custom post type I defined called 'Edition'

However, when I try to make a custom query to only call the custom post type, and no other posts... it just returns ALL posts regardless of the post_type definition i set:

<?php get_header(); ?>
<?php 
  $args = array(
          'post_type'      => 'edition',
          'posts_per_page' => -1
  );

  $allgemeine = new WP_Query( $args ); 
  while ( $allgemeine->have_posts() ) : $allgemeine->the_post(); 
    the_content();
  endwhile; ?>
  <?php wp_reset_postdata(); ?> 
<?php get_footer(); ?>

When I remove the pre_get_posts filter from mzy functions.php, the loop works perfect, but the custom post types do not show in the archive page.

Any ideas why this conflict is happening?

  • 写回答

3条回答 默认 最新

  • dongsi5381 2016-12-29 00:18
    关注

    A note to people who might use this, I had a problem with this filter:

    function my_get_posts( $query ) {
        if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
            $query->set( 'post_type', array( 'post', 'nav_menu_item', 'edition' ));
            return $query;
        }
    }
    add_filter( 'pre_get_posts', 'my_get_posts' );
    

    It worked for the most part but was causing a third party conflict with Advanced Custom Fields where it was breaking a search function through the database. I modified the logic to avoid modification to 3rd party get_posts calls:

    function my_get_posts( $query ) {
        if ( !is_admin() && $query->is_main_query() ) {
            if ($query->is_archive) {
                $query->set( 'post_type', array( 'post', 'nav_menu_item', 'edition' ) );
            return $query;
            }
        }
    }
    add_filter( 'pre_get_posts', 'my_get_posts' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号