0

I have gone through the process of creating a custom taxonomy + admin panel for a custom post type in my site. These taxonomies store various meta data in a wp_taxonomymeta table and use get_metadata/set_metadata to get and set the values of these fields. One of the fields in this taxonomy is an enabled/disabled flag.

I would like to globally exclude posts that have the disabled flag set to true. I am hooking into pre_get_posts, but I cannot figure out how to modify the query so that I can pull data in the wp_taxonomymeta for use in the query to exclude posts that should be hidden. Can anyone provide some insight into doing this? I could not find much helpful advice on google. If clarification is needed, please ask, I will do my best to clear anything confusing up.

EDIT FROM COMMENTS

I'm trying to modify the main query. The idea is ultimately to be able to hide/show globally across the site based on the taxonomy's meta data flag value. So I have a plugin that implements a pre_get_posts hook, but I cannot get the query figured out with the custom table (at least in the typical wordpress fashion, raw SQL would be cake, but I'm wanting to modify the main query).

There's not a lot of documentation around for working with these taxonomy tables since it seems to be a disputed way of doing stuff in wordpress.

2
  • Are you trying to modify the main query, or just creating a new custom query. Have a look at WP_Query's custom parameters so long. Jun 18, 2014 at 5:51
  • Trying to modify the main query. The idea is ultimately to be able to hide/show globally across the site based on the taxonomy's meta data flag value. So I have a plugin that implements a pre_get_posts hook, but I cannot get the query figured out with the custom table (at least in the typical wordpress fashion, raw SQL would be cake, but I'm wanting ot modify the main query). There's not a lot of documentation around for working with these taxonomy tables since it seems to be a disputed way of doing stuff in wordpress. :/
    – Cody Mays
    Jun 18, 2014 at 5:53

1 Answer 1

0

For those running into issues in the future, this is what I did:

I made my pre_get_posts function that queries the taxonomy as normal for testing if an taxonomy ID is not in a list:

            $q->set( 'tax_query', array(array(
                    'taxonomy' => 'tax',
                    'field' => 'id',
                    'terms' => tax_get_inactive(),
                    'operator' => 'NOT IN'
            )));

Then, I implemented my tax_get_inactive function as seen above, to query my taxonomymeta table and build an exclusion list. Not the most elegant way, but it works and the exclusion list can easily be cached if performance is in mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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