繁体   English   中英

WordPress-如何在wp_query中使用'category__not_in'

[英]Wordpress- how to use 'category__not_in' in wp_query

我有一个小问题。 我正在尝试从某些自定义帖子类型检索所有帖子,但不包括在类别ID 78中标记的帖子。

我有两个帖子。 其中一个仅标记为类别78,而另一个未标记为类别78。我签入了此类较旧的帖子,但对我来说,它不起作用。 我看到了所有结果。

这是我的代码:

 function data_fetch(){

    $the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'article', 'category__not_in ' => array( 78 ) ) );
    if( $the_query->have_posts() ) :
        while( $the_query->have_posts() ): $the_query->the_post(); ?>

            <h2><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></h2>

        <?php endwhile;
        wp_reset_postdata();
    endif;

    die();
}

这是帖子类型初始化查询功能:

    function article_cpt_init() {
    $labels = array(
        'name' => 'פריטי מאגר מידע',
        'singular_name' => 'פריט מאגר מידע',
        'add_new' => 'הוסף פריט מאגר מידע חדש',
        'add_new_item' => 'הוסף פריט מאגר מידע חדש',
        'edit_item' => 'ערוך פריט מאגר מידע',
        'new_item' => 'פריט מאגר מידע חדש',
        'all_items' => 'כל פריטי מאגר מידע',
        'view_item' => 'הצג פריט מאגר מידע',
        'search_items' => 'חפש פריט מאגר מידע',
        'not_found' =>  'לא נמצא פריט מאגר מידע',
        'not_found_in_trash' => 'לא נמצא פריט מאגר מידע בפח',
        'parent_item_colon' => '',
        'menu_name' => 'פריטי מאגר מידע',
    );

    $args = array(
        'labels' => $labels,
        'exclude_from_search' => true,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'article' ),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'taxonomies' => array('category', 'audience'),
        'menu_position' => null,
        'supports' => array( 'title', 'excerpt', 'editor' )
    );

    register_post_type( 'article', $args );
}
add_action( 'init', 'article_cpt_init');


/**
 * Register speciality Taxonomט
 */
function register_speciality_taxonomy() {
    $taxonomies = array(
        array(
            'slug'         => 'category',
            'single_name'  => 'נושא',
            'plural_name'  => 'נושאים',
            'post_type'    => 'article',
            'rewrite'      => array( 'slug' => 'נושאים' ),
        ),
        array(
            'slug'         => 'audience',
            'single_name'  => 'קהל יעד',
            'plural_name'  => 'קהלי יעד',
            'post_type'    => 'article',
            'rewrite'      => array( 'slug' => 'קהל יעד' ),
        ),
        array(
            'slug'         => 'tags',
            'single_name'  => 'תגיות',
            'plural_name'  => 'תגיות',
            'post_type'    => 'article',
            'rewrite'      => array( 'slug' => 'תגיות' ),
            'hierarchical' => false,
        )
    );
    foreach( $taxonomies as $taxonomy ) {
        $labels = array(
            'name' => $taxonomy['plural_name'],
            'singular_name' => $taxonomy['single_name'],
            'search_items' =>  'Search ' . $taxonomy['plural_name'],
            'all_items' => 'All ' . $taxonomy['plural_name'],
            'parent_item' => 'Parent ' . $taxonomy['single_name'],
            'parent_item_colon' => 'Parent ' . $taxonomy['single_name'] . ':',
            'edit_item' => 'Edit ' . $taxonomy['single_name'],
            'update_item' => 'Update ' . $taxonomy['single_name'],
            'add_new_item' => 'Add New ' . $taxonomy['single_name'],
            'new_item_name' => 'New ' . $taxonomy['single_name'] . ' Name',
            'menu_name' => $taxonomy['plural_name']
        );

        $rewrite = isset( $taxonomy['rewrite'] ) ? $taxonomy['rewrite'] : array( 'slug' => $taxonomy['slug'] );
        $hierarchical = isset( $taxonomy['hierarchical'] ) ? $taxonomy['hierarchical'] : true;

        register_taxonomy( $taxonomy['slug'], $taxonomy['post_type'], array(
            'hierarchical' => $hierarchical,
            'labels' => $labels,
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => $rewrite,
        ));
    }

}
add_action( 'init', 'register_speciality_taxonomy' );
// Display User IP in WordPress

我究竟做错了什么?

尝试这个:

function data_fetch(){
$args = array( 'posts_per_page' => -1, 
                's' => esc_attr( $_POST['keyword'] ), 
                'post_type' => 'article', 
                'tax_query' => array(
                        array(
                            'taxonomy' => 'audience',
                            'field'    => 'term_id',
                            'terms'    => array( 78 ),
                            'operator' => 'NOT IN',
                        ),
                    ), 
                );
    $the_query = new WP_Query($args);
    if( $the_query->have_posts() ) :
        while( $the_query->have_posts() ): $the_query->the_post(); ?>

            <h2><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></h2>

        <?php endwhile;
        wp_reset_postdata();
    endif;

    die();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM