0

I' having a custom post type with custom taxonomy. In the Post I've specified a post meta value (via update_post_meta) that has the value '0' or '1' (it's a checkbox).

Now I'm trying to generate a list of taxonomy values which have posts assigned to them and the post meta value is set to '1'.

Is there a way I can hook into wp_list_categories and pass post specific values as a filter parameter? Or any other way to achieve this?

  • Are you looking for a list of taxonomies? Or a list of posts? – mor7ifer Feb 17 '12 at 13:28
  • A list of taxonomies. – John Feb 17 '12 at 14:03
0

This will return an array of term objects, if you need these processed to be hierarchical or something, that's pretty easy, I think the challenge you were having was getting the term objects.

//get posts
$args = array(
    //basic stuff
    'post_status' => 'publish',
    //meta query
    'meta_query' => array(
        array(
            'key'     => 'your_key',
            'value'   => '1'
        )
    )
);
$posts = new WP_Query( $args );

//get categories from posts and amalgamate them
$categories = array();
foreach( $posts as $post ) {
    $new_cats = wp_get_object_terms( $post->ID, 'your-taxonomy-slug' );
    $categories = array_merge(
        $categories,
        array_diff(
            $categories,
            $new_cats
        )
    );
}

I feel like there might be something that you could do with the get_terms hook, but I can't put my finger on it (shy of querying for every term to see if it has posts that meet the condition, but that's way less efficient than my method I think).

  • Thank's! You definitely pointed me in the right direction. Your example returns an empty array and I think it's because the arrays from the wp_get_object_terms function always differ (eg. by [term_id]). Anyway I can push the values I need in a Array programmatically and it works like a charm! – John Feb 17 '12 at 16:00
  • Hurr, interesting...I didn't test it at all, just wrote the code right off and pasted it in, so it may be a dumb error...it might also be something to do with array_diff() not being able to compare objects...I'll make sure to do some testing if I ever need it! Glad you got it working! – mor7ifer Feb 17 '12 at 16:19

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.