Support » Plugins » Hacks » Count posts without assigned taxonomy

  • I’m designing knowledge base that has several custom post-types and several sub-categories in a taxonomy I called ‘sub’. In order for the post to show up in the proper location, a sub-category must be chosen, or the post will be floating around and not linked to from anywhere.

    Because of that, what I would like to do is run a query that will pull a list of posts (of any custom post-type), that are not associated with a taxonomy.

    I know how to pull a list of posts of a certain post-type and taxonomy, but I don’t know how to run a query based on what I’m looking for. I’m unsure if I can modify the below query to pull the information I’m looking for.

    	$loop = new WP_Query( array( 'post_type' => array('post-types'), 'sub' => 'sub-cats', 'posts_per_page' => 500, 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC' ) );
    	while ( $loop->have_posts() ) : $loop->the_post();
    		echo '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
    	endwhile; ?>

    Is this type of thing even possible in 3.0.4?

    Please forgive me if my terminology is unclear, I’m fairly new at this. Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz


    Is this a one time thing to so you can get all existing custom posts assigned to a sub-category? If not, you should setup a filter to not allow posts of custom types unless a sub-category is selected.

    I’m weak on using WP_Query, but I wonder if something like this will get what you’re looking for:

    WP_Query( array( 'post_type' => array('type1','type2','type3',...),
    'sub' => '', ...));

    If all else fails, write a SQL query that returns what you want. It gets complicated with taxonomies, but it will certainly get what you want.

    Thanks for your reply. A filter might be a better option, how would I go about setting this up?

    I tried

    WP_Query( array( 'post_type' => array('type1','type2','type3',...),
    'sub' => '', ...));


    WP_Query( array( 'post_type' => array('type1','type2','type3',...),
    'sub' => NULL, ...));

    however both resulted in pulling every post with the specified custom post-types in the database.

    I was hoping to use WP_Query as I’m not sure how to go about writing a SQL query that will pull exactly what I’m looking for.

    Moderator bcworkz


    I guess the problem with my idea is if there is no sub-category assigned, any ‘sub’ criteria is ignored. I’m guessing the taxonomy is not even assigned if there is no value to put into it? I’m not sure how to query, via WP or SQL, for something that is not there, but as I said, I’m weak at this.

    The trick with a filter is to find the right filter or action, that is part of the publish process, to hook from which it’ll be easy to check if a sub-category was selected. If not selected, it may be as simple as just throwing up an error page via wp_die(). The user then hit’s their back button, picks a sub-category, and then re-publishes.

    I’m still hoping someone can help me out with either a WP_Query or SQL query that will accomplish this. My ultimate goal is to build a page that will run this query and spit out the resulting post titles (with links to them) as sort of an after-the-fact error checking process. Does that make sense?

    bcworkz – thanks for your help so far. I’m comfortable writing the code to require a “sub” value, but am wondering if this would need to be in the post-new.php file, or perhaps post.php? Am I completely wrong?


    Moderator bcworkz


    To be a bit more clear, the filter which you would want to hook likely originates from one of those files, but your code would be part of your own plugin’s code or theme’s or child theme’s functions.php. If you need more help with this filter, you may wish to start a new thread so your original question doesn’t get obscured.

    To All:
    OP still needs help with writing a properly formed query for custom post types which have no taxonomy assigned, which is beyond my abilities to help.

    Thanks bcworkz, I’m going to create a new thread regarding requiring a taxonomy to be assigned before publishing is allowed.

    I’m still hoping to find a solution here so if anyone else has any advice I’m all ears!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Count posts without assigned taxonomy’ is closed to new replies.