I have a simple WP_query on a custom post and works fine:
$args = array(
'post_type' => 'post-type',
'showposts' => 2,
'location' => 'london'
);
but I want it to query two locations so used an array:
$args = array(
'post_type' => 'post-type',
'showposts' => 2,
'location' => array('london','paris')
);
but it only pulls in the last term. Is there another way that I should be doing this?
$args = array(
'post_type' => 'post-type',
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => array('london','paris')
)
)
);
Try this
Related
I am trying to get multiple taxonomy terms in WP_Query. Here is my code:
$category = get_field('portfolio_category'); //array of IDs like 14, 15, 16
<?php
$the_query = new WP_Query(array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projectCategories',
'field' => 'term_id',
'terms' => array( implode(', ', $category ) ),
'operator' => 'AND'
)
),
));
?>
The problem is in the current code I can only query the very first term.
For example I can only get projects from cat ID=14.
What am I doing wrong here? How can I query posts from multiple terms?
Thank you.
Since $category is already an array... change your terms value to just $category. so your full query would be:
$the_query = new WP_Query(array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projectCategories',
'field' => 'term_id',
'terms' => $category,
'operator' => 'AND'
)
),
));
I'm trying to pull all products located within a certain category slug.
I've tried the following, however both return every product in the store.
$products = wc_get_products( array( 'category' => array( 'Sony' ) ));
and
$productlist = wp_query(array( 'post_type' => 'product', 'product_cat' => 'Sony'));
Any pointers would be much appreciated.
Try this code
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => 12,// category ID
),
),
);
Try This:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'Sony', 'orderby' => 'rand' );
$data = new WP_Query( $args );
print_r($data);
Try with this and replace ENTER_CATEGORY with slug name or use simple 'terms' => 'slug/categoryname'
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array_map( 'sanitize_title', explode( ',', 'ENTER_CATEGORY' ) ),
'field' => 'slug',
'operator' => $atts['operator']
)
)
);
I'm hoping someone can help spot what the problem is. I have a custom taxonomy called "events_cat" I am trying to get all the post types "event" that are in the taxonomy term 11 to display, however the below code is pulling in events that are not in that taxonomy term and I can't see the mistake. Any ideas what might be causing the issue?:
<?php
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'events_cat',
'field' => 'term_id',
'terms' => array(11),
),
));
$upcomingEvents = new WP_Query($args); ?>
$custom_args=array(
'post_type' => "event",
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> -1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'tax_query' => array(
array(
'taxonomy' => 'events_cat',
'field' => 'id',
'terms' =>"11"
)
),
'orderby' => 'id',
'order' => 'ASC'
);
$custom_my_query = null;
$custom_my_query = new WP_Query($custom_args);
$custom_my_total_count = count($custom_my_query);
if( $custom_my_query->have_posts() )
{
while ($custom_my_query->have_posts()) : $custom_my_query->the_post();
?>
<?php echo get_the_title($post->ID);?>
<?php
endwhile;
}
wp_reset_query($custom_my_query); // Restore global post data stomped by the_post().
}
I need a tax_query that assigned to specific custom category and tags.
Below query showing all products but I need the products to a particular category.
Here tag="sale", category="look"
$args = array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'look' ),
),
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => 'sale',
),
),
);
$query = new WP_Query( $args );
You can simply use this
$new = new WP_Query('category_name=look&tag=sale');
try to use something like this
$args = array(
'posts_per_page' => 5,
'post_type' => array('videos','image','audio'),
'category_name' => 'look',
'tag'=>'sale'
);
$loop = new WP_Query( $args );
I have a custom post type called news. Inside that I have two custom taxonomies called press-releases and media-coverage. When I query for multiple posts with this query:
$args = array(
'post_type' => 'news',
'news_type' => 'press-releases',
'posts_per_page' => 15,
'paged' => $json_api->query->page,
);
$query = new WP_Query($args);
It only gets news posts categorized as press-releases like I want. But if I specify a slug that exists in media-coverage it returns that post even though I have the press-releases taxonomy specified. Example:
$args = array(
'name' => $slug,
'post_type' => 'news',
'news_type' => 'press-releases',
'posts_per_page' => 1,
);
You may try this
$args = array(
'post_type' => 'news',
'name' => '$slug,
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'news_type',
'field' => 'slug',
'terms' => 'press-releases'
)
)
);