I'm building a navigation, outside of the main loop, that includes drop downs. I have a custom post type called 'Events', that has its own categories. I would like there to be a drop down if there are posts within that custom post type and category, but I'm not sure what functions I should be using to determine this...

I have...

$hasposts = get_posts('post_type=Events&category=40');
if($hasposts) {
    ..// show the drop down menu

Should I even be using get_posts()? Everything I am getting returned has an empty array, but I know that some of those categories include posts...

Many thanks, WA.


It all boils down to WP_Query in the end even if you use get_posts, here's my modified version:

$hasposts = get_posts('post_type=sc-events&category=40');
if( !empty ( $hasposts ) ) {
    ..// show the drop down menu


$query = new WP_Query(array(
    'post_type' => 'sc-events',
    'category' => 40
if( $query->have_posts() ){
    echo 'we have posts';
} else {
    echo 'no posts found';

While this will work, there's an alternative inspired by your own answer that uses the category slug rather than its ID:

$term = get_term_by('name', 'whatever category 40 is called', 'category');
if($term != false ){
    if($term->count > 0 ){
        // we have posts
  • the 'Events' post type does have its own taxonomy, so the ID for the category is only specific to the custom post type. I agree with the hardcoding, but I'm modding someone else's code. The first solution you suggested though, it keeps returning an empty array, even when posts exist within that category. And the second suggestion... that's a little more involved than I was looking for because I wont' be actually outputting anything from that query. I just need to check if there are posts in the category. The input is very appreciated. – RCNeil Feb 14 '13 at 17:07
  • the second solution si identical to the first, it's what get_posts does internally, and they mean exactly the same thing and work exactly the same way. Just because you did a query, doesn't mean you have to do the loop – Tom J Nowell Feb 14 '13 at 17:09
  • Also, if you have a custom taxonomy, why are you using 'category=40' and not 'customtaxonomyname=40'? – Tom J Nowell Feb 14 '13 at 17:09
  • thank you for the input. It actually doesn't have a taxonomy, now that I'm digging through the functions.php file. It's using Categories from Post Types. So, even with all of that being said, why does either solution return an empty array for me, even if I just query by Category #, and don't pertain it to particular post types. Is that relevant? – RCNeil Feb 14 '13 at 17:23
  • This suggest missing information, can you post the registration code for your post type? – Tom J Nowell Feb 14 '13 at 17:34

If you want to have an array with all the terms inside your custom taxonomy, with the additional condition that they contain at least on post, this is the function you're looking for:

        'hide_empty' => true

This will retrieve information regarding the terms, not the posts themselves. The only thing you know about posts in this scenario is that they exist within those terms, nothing else. If the dropdown menus will display some kind of information regarding the posts, maybe you'll need something other than the above. Otherwise, get_terms is the function you're looking for.


I was able to perform this conditional check through the following code -

if (get_category('40')->category_count > 0) {
    ..//show the drop down
  • 1
    This will fail if the term 40 is deleted or its ID changes. – Tom J Nowell Feb 14 '13 at 17:34

There is no reason to return the full get_posts object you can keep things better optimized by using the fields parameter to return the ID's only

$posts = get_posts( array(
    'post_type' => 'Events',
    'posts_per_page' => 1,
    'fields' => 'ids',
    'tax_query' => array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( '40' )
) );
if ( ! empty ( $posts ) ) {
    ..// show the drop down menu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.