'How get terms and posts based on TERM meta key?
I need to add meta data to terms (something like update_post_meta).
I have created wp_termmeta (using this tutorial with some modifications) table like wp_postmeta.
So now this mechanism worked good and i may add/get/delete meta data to any term using get_metadata / update_metadata / delete_metadata.

I may get term meta data based on term object / id etc. But i need to get terms/posts based on term meta key.
For example:
I have CPT book. Then i have custom tax writer. Now i am adding meta data to writer - featured with possible values true/false (1/0).
So now i have 2 questions:
1) How to get all writers (terms from writer tax) which are featured (featured = true)?
2) Ho to get books from CPT book belongs to writers which are featured?
P.S. 1
Here are get_posts function. Using args i may get posts based on posts meta key:
<?php $args = array(
'meta_key' => '',
'meta_value' => ''); ?>
Also there are get_terms function but without meta key parameters.
Maybe there are some solution in WP_Query to get things i need? Maybe there are no built-in solution for my needs?
Solution 1:[1]
How to get all writers (terms from writer tax) which are featured (featured = true)?
For this you first need to grab the ID's of the featured terms, and then grab those terms.
$featured_writers = get_featured_writers();
How to get books from CPT book with writers who are featured?
For this I recommend first grabbing the ID's of the featured terms, and then creating a WP_Query object of books who are related to that term. You can then use this WP_Query object in The Loop.
$featured_books = get_books_with_featured_writer();
If you want both featured writers and books with featured writers
To save duplicate queries (although WP is quite good at caching them anyway), you could do this if you require both the featured writers and the books with featured writers -
$featured_writers_ids = get_featured_writer_ids();
$featured_writers = get_featured_writers($featured_writers_ids);
$featured_books = get_books_with_featured_writer($featured_writers_ids);
The code
Put this code in your functions.php files (or any file included by functions.php).
Give it a try and let me know if you get problems -
function get_featured_writers($featured_writers_ids = null){
/** Ensure the $featured_writers_ids are set and valid */
$featured_writers_ids = check_features_writer_ids($featured_writers_ids);
if(!$featured_writers_ids) :
return false;
endif;
$args = array(
'fields' => 'ids',
'include' => $featured_writers_ids
);
$featured_writers = get_terms('writer', array('include' => $featured_writers_ids));
return $featured_writers;
}
function get_books_with_featured_writer($featured_writers_ids = null){
/** Ensure the $featured_writers_ids are set and valid */
$featured_writers_ids = check_features_writer_ids($featured_writers_ids);
if(!$featured_writers_ids) :
return false;
endif;
$args = array(
'post_type' => 'book',
'tax_query' => array(
array(
'taxonomy' => 'writer',
'field' => 'id',
'terms' => $featured_writers_ids
)
)
);
$books = new WP_Query($args);
return $books;
}
function get_featured_writer_ids(){
global $wpdb;
// This query assumes you've added `termmeta` to the `$wpdb` global. Replace with `$wpdb->termmeta` with `'wp_termmeta'` if you have not.
$query = $wpdb->prepare("SELECT `%1$s`.`term_id` FROM %1$s WHERE `meta_key` = 'term_featured' AND `meta_value` = '1'", $wpdb->termmeta);
$featured_writers_ids = $wpdb->get_col($query);
return $featured_writers_ids;
}
function check_features_writer_ids($featured_writers_ids = null){
/** Check to see if any featured writers were passed, or if they should be grabbed now */
if(is_null($featured_writers_ids)) :
$featured_writers_ids = get_featured_writer_ids();
endif;
/** Ensure that there are featured writers, and that $featured_writers_ids is an array */
if(is_empty($featured_writers_ids) || !is_array($featured_writers_ids)) :
return false;
endif;
return $featured_writers_ids;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
