最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - find posts that don't have a custom taxonomy

programmeradmin2浏览0评论

I am using a custom taxonomy series to keep track of posts that are in a series. I would like to find the posts that do not have a series. I have the following query:

global $wpdb;
$pr = $wpdb->prefix;    

$sql_no_series=
    "Select  
            *
    from    
            wp_term_taxonomy tt,
            wp_term_relationships tr,
            wp_posts p 
    WHERE   
            tt.term_taxonomy_id=tr.term_taxonomy_id AND
            tr.object_id=p.ID AND
            p.ID NOT IN 
                (Select  
                        p.ID
                from    
                        wp_term_taxonomy tt,
                        wp_term_relationships tr,
                        wp_posts p 
                WHERE   
                        tt.term_taxonomy_id=tr.term_taxonomy_id AND
                        tr.object_id=p.ID AND
                        tt.taxonomy='series')
    AND tt.term_taxonomy_id = $category ";

$no_series = $wpdb->get_results($sql_no_series,ARRAY_A);

Is there a way to do it using WP_Query?

I am using a custom taxonomy series to keep track of posts that are in a series. I would like to find the posts that do not have a series. I have the following query:

global $wpdb;
$pr = $wpdb->prefix;    

$sql_no_series=
    "Select  
            *
    from    
            wp_term_taxonomy tt,
            wp_term_relationships tr,
            wp_posts p 
    WHERE   
            tt.term_taxonomy_id=tr.term_taxonomy_id AND
            tr.object_id=p.ID AND
            p.ID NOT IN 
                (Select  
                        p.ID
                from    
                        wp_term_taxonomy tt,
                        wp_term_relationships tr,
                        wp_posts p 
                WHERE   
                        tt.term_taxonomy_id=tr.term_taxonomy_id AND
                        tr.object_id=p.ID AND
                        tt.taxonomy='series')
    AND tt.term_taxonomy_id = $category ";

$no_series = $wpdb->get_results($sql_no_series,ARRAY_A);

Is there a way to do it using WP_Query?

Share Improve this question asked Jun 8, 2016 at 19:17 pppppppp 1012 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

Emily's answer is mostly correct, just that the operator shouldn't have an underscore in it:

$args = array(
    'post_type' => 'my_post_type',
    'tax_query' => array(
        array(
            'taxonomy' => 'series',
            'operator' => 'NOT EXISTS',
        ),
    ),
);
$query = new WP_Query( $args );

Something like this should work, where series is the taxonomy name and not a option within a taxonomy:

$args = array(
    'post_type' => 'my_post_type',
    'tax_query' => array(
        array(
            'taxonomy' => 'series',
            'operator' => 'NOT_EXISTS',
        ),
    ),
);
$query = new WP_Query( $args );

reference with plenty of code examples: https://codex.wordpress/Class_Reference/WP_Query#Taxonomy_Parameters

发布评论

评论列表(0)

  1. 暂无评论