Home › Forums › Front-end Issues › Query posts for a relationship value fallback issues
Hi,
I am using the meta_query LIKE to match an author value “Joe Blogs” to the database value stored on in a relationship field, however If there is no match within the LIKE comparison, the meta_query still produces a wild card result similar to if the comparison condition was not set at all.
If am trying to find a possible solution that if no result is provided from the meta_query then it would fall back to a tax_query so as a result wold always be possible.
$articles = new WP_Query(
array(
'post_type' => 'article',
'orderby' => 'posted_date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => $totalRecordsToDisplay,
'meta_query' => array (
array(
'key' => 'authors', // relationship field
'value' => '"' . get_the_ID() . '"',
//'value' => $post->post_title,
//'value' => get_the_id(),
'compare' => 'LIKE'
)
),
'tax_query' => array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'expertise',
'field' => 'term_id',
'terms' => array(join(",", wp_list_pluck($expertises, 'term_id'))),
'include_children' => true
)
)
),
)
);
As far as I know there isn’t any way to do an ‘OR’ on meta_query OR tax_query. Also, as far as I know this would be an ‘AND’ relationship.
Your meta query should not return results if there are not matches.
You would need to do 2 queries. If the first does not return results than do a second with the tax_query.
Thanks for the suggestion, I resolved this by merging the two queries.
function get_post_primary_category($post_id, $term='category', $return_all_categories=false){
$return = array();
if (class_exists('WPSEO_Primary_Term')){
// Show Primary category by Yoast if it is enabled & set
$wpseo_primary_term = new WPSEO_Primary_Term( $term, $post_id );
$primary_term = get_term($wpseo_primary_term->get_primary_term());
if (!is_wp_error($primary_term)){
$return['primary_category'] = $primary_term;
}
}
if (empty($return['primary_category']) || $return_all_categories){
$categories_list = get_the_terms($post_id, $term);
if (empty($return['primary_category']) && !empty($categories_list)){
$return['primary_category'] = $categories_list[0]; //get the first category
}
if ($return_all_categories){
$return['all_categories'] = array();
if (!empty($categories_list)){
foreach($categories_list as &$category){
$return['all_categories'][] = $category->term_id;
}
}
}
}
return $return;
}
$args = array (
'post_type' => 'article',
'orderby' => 'posted_date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => $totalRecordsToDisplay,
'meta_query' => array (
array(
'key' => 'authors',
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
);
$articles = new WP_Query( $args );
if ($articles->post_count < $totalRecordsToDisplay ) {
$new_TotalRecordsToDisplay = $totalRecordsToDisplay - $articles->post_count;
$article_expertise = get_post_primary_category($post->ID, 'expertise');
$primary_expertise = $article_expertise['primary_category'];
//echo $primary_expertise->term_id;
$args = array(
'post_type' => 'article',
'orderby' => 'posted_date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => $new_TotalRecordsToDisplay,
'tax_query' => array(
array(
'taxonomy' => 'expertise',
'field' => 'term_id',
'terms' => $primary_expertise->term_id,
//'terms' => array(join(",", wp_list_pluck($expertises, 'term_id'))),
'include_children' => true
)
)
);
$more_query = new WP_Query( $args );
$articles->posts = array_merge( $articles->posts, $more_query->posts );
$articles->post_count = count( $articles->posts );
}
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.