The obvious way is to iterate through the resulting array of IDs, get_post for each and test against post_status == 'publish'
. But I wonder whether this could cause memory issues since get_post
will attempt by default to cache each result? Short of a custom SQL join, are there any surprise args one can pass to get_objects_in_term()
or is there some other tax function I’m not leveraging that I should be?
Elegant way to include only published posts with get_objects_in_term()?
![placeholder](https://olatech.pro/community/wp-content/uploads/2023/06/placeholder.jpeg)
You can add
'post_status' => 'publish'
in your query to retrieve only objects with status publish , this will work forget_posts
,query_posts
or$wp_query
and to include also custom taxonomies you can usetax_query
in your args listThere’s no arguments you can pass in. The only argument that does get used is
order
. Here’s the source of the function:You can, however, copy the function and add in an additional clause to use the post status.
Not much different. An additional element in the
$defaults
as well as some modifications to the SQL.