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',
),
)
);
}
Related
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();
}
);
I am trying to query posts according to their post format. Currently, I have the video, gallery, and standard post formats on my site. It's quite easy to query these post formats individually. However, I have a combination of tax queries of 10 posts that are standard and video post format, my query fails.
Here's the code I have right now:
$args = array(
'post_status' => 'publish',
'orderby' => 'date',
'posts_per_page' => 10,
'relation' => 'OR',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'IN',
'terms' => array( 'post-format-video' )
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video', 'post-format-gallery' ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
I think, you should put the 'relation' => 'OR', into the 'tax_query'-Array.
You are adding 'relation' => 'OR' in the wrong place. it should be inside the tax_query array. check the below code.
$args = array(
'post_status' => 'publish',
'orderby' => 'date',
'posts_per_page' => 10,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'IN',
'terms' => array( 'post-format-video' )
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video', 'post-format-gallery' ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
USEFUL LINKS
taxonomy-parameters
I need to fetch posts from "blog" category but they should not contain certain taxonomy "offers".
What I did was
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'tax_query' => array(
'relation' => 'AND',
array(
'cat' => $category->term_id,
'category__in' => $category->term_id,
),
array(
'taxonomy' => 'offers',
'operator' => 'NOT EXISTS'
),
),
);
But I can't seem to get the correct posts. Is theabove method wrong?
I think you should try this way but i am not sure this will work:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'fields' => 'term_id',
'terms' => $category->term_id,
),
array(
'taxonomy' => 'offers',
'operator' => 'NOT IN'
),
),
);
I am using a query in my template.php file to display wooCommerce products by category.
My query looks like this:
<?php
$args = array(
'post_type' => 'product',
'stock' => 1,
'posts_per_page' => 99,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'system_components',
'menu_order' => 'asc'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'add-ons',
'menu_order' => 'asc'
)
),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => '!='
)
));
$loop = new WP_Query($args);
if ($loop->have_posts()) {
while ($loop->have_posts()) :
$loop->the_post(); ?>
.....
This works, and I can see the products I want and the one I don't want are not visible.
The only bit I don't understand is that 'menu_order' => 'ASC' doesn't seem to work.
I doesn't' matter what I type as menu order in the product settings, the order doesn't change.
What am I doing wrong here?
Thanks
Remove 'menu_order' => 'asc' part from tax_query and add to main, that should work:
$args = array(
'post_type' => 'product',
'stock' => 1,
'posts_per_page' => 99,
'orderby' => 'menu_order',
'order' => 'ACS',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'system_components'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'add-ons'
)
),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => '!='
)
));
As you can see in the documentation, the tax_query hasn't parameter menu_order.
Please, Set page order in your product.
For this go to Admin panel. Click on Products menu from left menu, Then edit product and set page order.
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