The code below is for a taxonomy template (taxonomy-city.php), within each post I have two taxonomies City and Region.
I'm trying to show posts that share the same region as the city. eg if I'm on the London page I want to show other cities that have the Europe taxonomy term selected.
<?php
$today = date('Ymd');
$term = $wp_query->queried_object;
$getterm = $term->slug;
$args = array(
'posts_per_page' => '9',
'order' => 'ASC',
'orderby' => 'meta_value_num',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $getterm,
'include_children' => true,
'operator' => 'IN'
),
array(
'taxonomy' => 'region',
'field' => 'slug',
'terms' => array( 'europe', 'asia-pacific', 'north-america' ),
)
),
'meta_query' => array(
array(
'key' => 'date',
'compare' => '>=',
'value' => $today,
)
),
);
$query = new WP_Query( $args );
?>
You haven't actually said what the problem is. However, if you want the search to be, eg "London" in "Europe", then your relation should be set to AND. Also, you should add 'operator' => 'IN' to your region tax parameters. I know it's the default setting, but it doesn't hurt to explicitly declare it.
Related
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',
),
)
);
}
I need to sort products for a specific category in WooCommerce by sky. Tried different combinations but non of work. Like I tried the bellow
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => 'sku',
'order' => 'asc',
'posts_per_page' => $atts['per_page'],
'meta_query' => $meta_query,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'terms' => array_map('sanitize_title', explode(',', $atts['category'])),
'field' => 'slug',
'operator' => $atts['operator']
),
array(
'taxonomy' => 'product_tag',
'terms' => array_map('sanitize_title', explode(',', $atts['tags'])),
'field' => 'slug',
'operator' => $atts['operator']
)
)
);
$products = new WP_Query($args);
Where $attrs[] data are coming from shortcode parameters. But it does not sort product by sku ?
A help will be greatly appreciated.
The product sku (_sku) is saved as meta value, thus you cant simply say "order by sku", you should order by meta value, and set the meta keyto _sku. If they do not have a sku set the products will not be displayed.
Example:
args = array(
'post_type' => 'product',
'meta_key' => '_sku',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_sku',
'compare' => 'EXISTS',
),
),
);
If your sku is numeric you should use "meta_value_num" if the sku's are strings then use "meta_value" instead.
You can read more about it here -> https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
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 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',
);
The following code is supposed to get posts that do not have specific terms in a custom taxonomy. At the moment it still gets them. Is something missing.
$args = array(
'numberposts' => '3',
'post__not_in' => $post_not_in,
'tax_query' => array(
'taxonomy' => 'topic',
'terms' => 9,
'field' => 'id',
'operator' => 'NOT IN'
)
);
$extras = get_posts($args);
Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays)
— Wordpress Codex on Taxonomy Parameters
Have you tried?
$args = array(
'numberposts' => '3',
'post__not_in' => $post_not_in,
'tax_query' => array(
array(
'taxonomy' => 'topic',
'terms' => 9,
'field' => 'id',
'operator' => 'NOT IN'
)
)
);
$extras = get_posts($args);