• I’m currently trying to create a simple search of a custom post type for job vacancies and the search includes using a couple of the custom taxonomies. I’ve used get_terms() to generate a drop down for each of the taxonomies, and by default it should only display terms that have posts in.

    Trouble is, this includes posts in the trash. As the posts may be used in the future again, they’re not going to be deleted, however, for example, there is a post/job with a location set as London, so London appears in the list of options, but when you select it (and nothing else), no results appear because the job is in the trash.

    Is there any way to filter the get_terms() results easily to reduce the list to only have those with live posts as opposed to all posts? At present the only solution I can think of is to loop through each term (which I do anyway) and then check some how to see if there is at least one live post using it, which is quite a lot of processing for a simple drop down list.

    Any suggestions welcome ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • setting hide_empty to true doesnt work for this?

    Thread Starter Sarah Anderson

    (@sarahg111)

    No. Although the posts are in trash, they are still classed as having those terms, and so the count for posts using the terms include them. If you go into any taxonomy page (eg. categories), and check a category with no live posts in but one in the trash, you’ll even see in there that it says there is a post in it, but clicking on the number will take you to the edit posts page showing no posts for that category.

    It’s just a bit frustrating but I can understand the logic. Just wondered if there is a function I’ve missed that can do what I need rather than creating my own.

    prolly aughta make your own… although i am not sure this wouldnt be a bug to report.

    Thread Starter Sarah Anderson

    (@sarahg111)

    Thanks ??

    Well I’ve used get_posts() to check for a live post using

    $apost = get_posts('numberposts=1&jv_location='.$term->slug.'&post_type=my_jobs&post_status=publish');

    But this slows the initial page load down noticeably so it’s not an ideal longterm solution.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘get_terms() – returning only terms with live posts’ is closed to new replies.