How to get a meta values from all posts in a special category
-
Hi, I want to display all values of a meta key (custom field).
With this function I can display all the values, but I can’t specify a category:function get_meta_values( $key = '', $type = 'post', $status = 'publish' ) { global $wpdb; if( empty( $key ) ) return; $r = $wpdb->get_results( $wpdb->prepare( " SELECT p.ID, pm.meta_value FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE pm.meta_key = '%s' AND p.post_status = '%s' AND p.post_type = '%s' ", $key, $status, $type )); foreach ( $r as $my_r ) $metas[$my_r->ID] = $my_r->meta_value; return $metas; }
I found this Code-Snippet for making a query for a special category:
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) WHERE $wpdb->terms.name = 'mycategory_slug' AND $wpdb->term_taxonomy.taxonomy = 'category'
Anyone an Idea how to put these two function together? I want to put in the cat_ID instead of the slug. Thanks in advance!
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘How to get a meta values from all posts in a special category’ is closed to new replies.