Get all terms from posts that have a certain post meta value - wordpress

I am trying to get all terms from a custom post type (artist) that have a meta_key(performance_date_1) and a meta_value(20220430) that I created with ACF.
This is what I have tried so far.
$events = get_terms( 'artist_event', array(
'post_type' => 'artist',
'taxonomy' => 'artist_event',
'hide_empty' => false,
'suppress_filters' => false,
'meta_query' => array(
// 'relation' => 'OR',
array(
'key' => 'performance_date_1',
'compare' => '=',
'value' => '20220430',
)
),
) );
When using get_terms is the meta_query looking in taxonomy fields or post fields? I need the meta query to look for post fields to fix this issue I believe.

I had a similar situation. In my case, I resorted to using array_filter against posts via WP_Query::have_posts although this is certainly not the most performant solution.
$events = array_filter(
get_terms(array('taxonomy' => 'artist_event')),
function($term) {
return (new WP_Query(array(
'post_type' => 'artist',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'artist_event',
'field' => 'slug',
'terms' => $term->slug,
),
'meta_query' => array(
array(
'key' => 'performance_date_1',
'compare' => '=',
'value' => '20220430',
)
),
)))->have_posts();
}
);

Related

Can you use tax_query with multiple post types in wp_query?

Let's say I'm querying two post types, like so:
$args = array(
'post_type' => array('post', 'another_post_type'),
'tax_query' => array(
array(
'taxonomy' => 'custom-taxonomy',
'field' => 'slug',
'terms' => 'test-slug',
)
)
);
If I have custom-taxonomy linked to another_post_type, how would I run this query so that the tax_query only ran for the another_post_type posts? Basically I want the query to return all regular post, but only another_post_type posts with the category of test-slug.
Is this possible?
This seems to be working:
$args = array(
'post_type' => array('post', 'another_post_type'),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'custom-taxonomy',
'field' => 'slug',
'terms' => 'test-slug'
),
array(
'taxonomy' => 'category', # default post category
'operator' => 'EXISTS'
)
)
);

WP Query: How to get posts from a specific author OR those with a specific meta value

I would like to retrieve all those posts whose author (post table field) is a given one OR those which has a given meta value (postmeda table field).
If "author" was a meta value, I know I could use a meta_query to achieve it. The thing here is that it is not... so I think I cannot use the "author" field within a meta_query and use the "relation" key.
I'm looking for something like:
$args = array(
'post_type' => array('post'),
'orderby' => 'ASC',
'order' => 'date',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'field' => 'author',
'value' => $author_id,
'compare' => '==',
),
array(
'key' => '_meta_field_name',
'compare' => 'NOT EXISTS',
),
),
array(
'relation' => 'AND',
array(
'key' => '_meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => '_meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
),
);
$data = new WP_Query( $args );
Any suggestion on how to achieve that using WP_Query?
Thanks!
You might like to try an approach like this one instead. The idea is to query the two conditions you want to search for, then merge the two queries into one finished product.
//Get posts with the author you're looking for
$args1 = array(
'author_name' => 'testuser', //or 'author' => $author_id or something else
);
$data1 = get_posts( $args1 );
//Get posts with the meta data you're looking for
$args2 = array(
'meta_query' => array(
array(
'key' => 'meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => 'meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
);
$data2 = get_posts( $args2 );
//Merge both arrays
$allData = array_merge( $data1, $data2 );
//Get just the IDs of all the posts found, while also dropping any duplicates
$postIDs = array_unique( wp_list_pluck( $allData, 'ID' ) );
//Do a new query with these IDs to get a properly sorted array of post objects
$args3 = array(
'post__in' => $postIDs,
'order' => 'ASC',
'orderby' => 'date',
);
$finalAnswer = get_posts( $args3 ); //This is your array of post objects. Ta-Da!

How to make query with three different parameters

I have three different parameters to search with, first one is post title of custom post type, second is taxonomy term and third is post type also, I am able to search if three of them are selected, but how to search related records if two of them are selected only or just one of them ?
$args = array(
'post_type' => 'course',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'course-category',
'terms' => $cs_course_cate,
'field' => 'term_id',
)
),
'meta_query' => array(
array(
'key' => 'cs_course_campus_id',
'value' => $cs_campus,
'compare' => 'LIKE',
),
),
);
View Search Box Here
Try this code.
$args = array(
'post_type' => 'course',
'post_status' => 'publish'
);
if(!empty($cs_course_cate)){
$args = array('tax_query' => array(
array(
'taxonomy' => 'course-category',
'terms' => $cs_course_cate,
'field' => 'term_id',
)
));
}
if(!empty($cs_campus)){
$args = array('meta_query' => array(
array(
'key' => 'cs_course_campus_id',
'value' => $cs_campus,
'compare' => 'LIKE',
),
)
);
}

WP_Query multiple post type and taxonomies

I would like to get results from two post types,
1) Post, only show 'business' kicker
2) local-news, show all kickers
I've so far:
$args=array(
'cat' => $my_category_id,
'post_status' => 'publish',
'posts_per_page' => 5,
'post_type' => array('post', 'local-news'),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'postkicker',
'term' => 'business'
),
array(
'taxonomy' => 'impactkicker',
),
),
'orderby' => 'date',
'order' => 'DESC'
);
Currently is not showing both post types, any suggestions? Thanks in advance
You need to make sure you've connected your cpt local-news to category taxonomy as well, because you are trying to select by this taxonomy. Here's extensively commented approach which will do what you need i guess. At least if I got your idea clearly. You can change tax_query main relation to OR instead of AND to output items if they don't have category set as well.
$args = array(
// 'cat' => $my_category_id, // better replace it with category in tax_query, see below.
'post_status' => 'publish',
'posts_per_page' => 5,
'post_type' => array( 'post', 'local-news' ),
'tax_query' => array(
'relation' => 'AND', // we set it to AND because we want all posts of this category i guess.
array(
'taxonomy' => 'category',
'term' => $my_category_id,
'field' => 'term_id',
'operator' => 'IN', // just to be more explicit.
),
array( // we create nested sub queries which will filter by other 2 taxonomies, which in turn has OR relation.
'relation' => 'OR',
array(
'taxonomy' => 'postkicker',
'field' => 'slug', // by default it's term_id and you are passing in a slug so set it explicitly.
'term' => 'business',
'operator' => 'IN', // just to be more explicit.
),
array(
'taxonomy' => 'impactkicker',
'field' => 'slug', // set these or not add rule for taxonomy at all.
'term' => 'your-term-slug', // same here.
'operator' => 'IN', // it's a default value, but ou can set 'EXISTS' if you need to check if whatever term of such taxonomy is assigned.
),
),
),
'orderby' => 'date',
'order' => 'DESC',
);

Dynamic meta_query

I'm breaking my head over here. Through a post on this website I've managed to create a custom taxonomy archive Page in WordPress. Now I'm trying to add dynamic checkbox filters to it, but I can't seem to get the meta_query working.
This line of code works like I would like it to work;
$query = array(
'post_type' => 'company',
'posts_per_page' => 999,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'company_category',
'field' => 'slug',
'terms' => $al_cat_slug
)
),
'meta_query' => array (
array (
'key' => 'company_method',
'value' => 'Online',
'compare' => 'LIKE',
)
)
);
How ever, this one won't:
$query = array(
'post_type' => 'company',
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'company_category',
'field' => 'slug',
'terms' => $al_cat_slug
)
),
);
$al_tax_post_qry = new WP_Query($query);
$meta_query = $al_tax_post_qry->get('meta_query');
$name = 'company_method';
$value = explode(',', $_GET[ $name ]);
$meta_query[] = array(
'key' => $name,
'value' => $value,
'compare' => 'LIKE',
);
$al_tax_post_qry->set('meta_query', $meta_query);
Whatever I enter in the URL, it keeps finding all the results and it won't filter like the first. A print_r($meta_query); gives me:
Array ( [0] => Array ( [key] => company_method [value] => Array ( [0] => Online ) [compare] => LIKE ) )
Edit 07-06-2016 // 09:00
After reading the comment stating that I should use 'IN', I've experimented a little further and when I use it withing the query itself, it gives me no results at all. It seems 'IN' is the issue.
The field I'm querying is an 'Advanced Custom Fields' field so that might have something to do with it? However the examples on their website also use the same method.
Not Working:
$query = array(
'post_type' => 'company',
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'company_category',
'field' => 'slug',
'terms' => $al_cat_slug
)
),
'meta_query' => array (
'relation' => 'AND',
array (
'key' => 'company_method',
'value' => array('online', 'orange', 'apple'),
'compare' => 'IN',
)
),
);
Working:
$query = array(
'post_type' => 'company',
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'company_category',
'field' => 'slug',
'terms' => $al_cat_slug
)
),
'meta_query' => array (
'relation' => 'AND',
array (
'key' => 'company_method',
'value' => 'online',
'compare' => 'LIKE',
)
),
);
Optionally I could create an array entry in the meta_query for every value in the array, but that might not be ideal.
If you define your $meta_query value as an array, you should change your compare operator. IN and NOT IN are the array specific ones.
Try this:
$meta_query[] = array(
'key' => $name,
'value' => $value,
'compare' => 'IN',
);
Hope it helps!
With help from this post, the issue is solved! Values are stored in a serialized way, so need a little different approach.
https://wordpress.stackexchange.com/questions/183182/meta-query-compare-in-not-working

Resources