我与自定义帖子类型存在冲突问题。在我的存档页面上,自定义帖子类型未显示常规帖子,因此我必须添加此pre_get_posts
过滤器:
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;
}
}
这就是我想要它做的,并且在存档页面中显示了常规帖子,以及我定义的名为'Edition'的自定义帖子类型
然而,当我尝试使自定义查询只调用自定义帖子类型,而没有其他帖子...它只返回所有帖子,无论我设置的post_type定义如何:
<?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(); ?>
当我从mzy functions.php
中删除pre_get_posts过滤器时,循环工作正常,但自定义帖子类型不会显示在存档页面中。
为什么会发生这种冲突?
答案 0 :(得分:1)
更新您的过滤器:
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' );
答案 1 :(得分:0)
<?php
/*
* Creating a function to create our CPT
*/
function custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Editions', 'Post Type General Name' ),
'singular_name' => _x( 'Edtiion', 'Post Type Singular Name'),
'menu_name' => __( 'Editions'),
'parent_item_colon' => __( 'Parent Edtiion'),
'all_items' => __( 'All Editions'),
'view_item' => __( 'View Edition'),
'add_new_item' => __( 'Add New Editions'),
'add_new' => __( 'Add New'),
'edit_item' => __( 'Edit Edition'),
'update_item' => __( 'Update Edition'),
'search_items' => __( 'Search Edition'),
'not_found' => __( 'Not Found'),
'not_found_in_trash' => __( 'Not found in Trash'),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'edition' ),
'description' => __( 'Editions and edition specific data' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'post_tag','edition'),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
// Registering your Custom Post Type
register_post_type( 'edition', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'custom_post_type', 0 );
function themes_taxonomy() {
register_taxonomy(
'edition', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'edition', //post type name
array(
'hierarchical' => true,
'label' => 'Edition store', //Display name
'query_var' => true,
'show_ui' => true,
'show_admin_column' => true,
'rewrite' => array(
'slug' => 'edition', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'themes_taxonomy');
?>
答案 2 :(得分:-1)
对于可能使用此功能的用户的说明,我对此过滤器有疑问:
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' );
它在很大程度上起作用,但是导致与高级自定义字段发生第三方冲突,它通过数据库破坏了搜索功能。我修改了逻辑以避免修改第三方get_posts调用:
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' );