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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745180454a4615393.html

相关推荐

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

    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

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信