Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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
}

or

$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
    }
}
share|improve this answer
    
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:

get_terms(
    'group',
    array(
        '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.

share|improve this answer

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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