I need to figure out how to query posts by the following conditions:
if they are in the category (by ID) or if they have a taxonomy term. So that on one archive page, I have all posts with a category ID of 12, and all posts that have the taxonomy term 'archive-trading-tools' I have set up custom taxonomy and that part is working. I am stuck at:
<?php
$query_args = array(
'post_type' => array('post', 'webinar'),
'posts_per_page' => 10,
'paged' => $paged,
'page' => $paged,
'cat' => 12,
//'relation' => 'OR'
);
$query_args['tax_query'] = array (
'taxonomy' => 'archive-categories',
'term' => 'archive-trading-tools'
);
$the_query = new WP_Query( $query_args );
?>
In WordPress a category is a built-in taxonomy. Then in theory, the following should work.
$args = array(
'post_type' => array('post', 'webinar'),
'posts_per_page' => 10,
'paged' => $paged,
'page' => $paged,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'archive-categories',
'field' => 'slug'
'terms' => 'archive-trading-tools',
),
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => 12
)
)
);
$query = new WP_Query( $args );
To expand on #depiction's answer, you the 'terms' in the tax_query should be "term" instead of "terms". No 's at the end.
$args = array(
'post_type' => array('post', 'webinar'),
'posts_per_page' => 10,
'paged' => $paged,
'page' => $paged,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'archive-categories',
'field' => 'slug'
'terms' => 'archive-trading-tools',
),
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => 12
)
)
);
$query = new WP_Query( $args );
Related
I use the array to show all products and posts on the same page:
$args = array(
'post_type' => array( 'product', 'post' ),
'posts_per_page' => 999,
'orderby' => 'meta_value_num',
'meta_key' => 'score_value',
'order' => 'DESC'
);
$custom_query = new WP_Query($args);
Now I want to filter posts 'cat' => '82,85' and products 'product_cat' => 96.
I have added the code snippes to my code, but after no post or products are shown.
$args = array(
'post_type' => array( 'product', 'post' ),
'cat' => '82,85',
'product_cat' => 96,
'posts_per_page' => 999,
'orderby' => 'meta_value_num',
'meta_key' => 'score_value',
'order' => 'DESC'
);
$custom_query = new WP_Query($args);
How should I modify my code that both post types and all posts and products within the categories will be displayed?
Thank you,
Mags
You can do that by using tax_query.
$args = array(
'post_type' => array('product', 'post'),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => ['82', '85'],
),
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => ['96'],
),
),
'posts_per_page' => 999,
'orderby' => 'meta_value_num',
'meta_key' => 'score_value',
'order' => 'DESC',
);
$custom_query = new WP_Query($args);
I currently have a search bar on my category page which will allow the search of either the product title (custom post type) or a taxonomy of brand. I can get both of these to work independently, passing in either arguments to the WP_Query, however if I attempt to array merge them both together, neither work.
What I have is:
$search = sanitize_text_field($_POST['productsearch']);
$post_category = $_POST['post_cat'];
$cats = array(
'taxonomy' => 'productscat',
'field' => 'id',
'key' => 'name',
'terms' => $post_category,
);
$args = array(
'post_type' => 'products', //custom post type
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
's' => $search,
'tax_query' => $cats,
'posts_per_page' => -1,
);
$args2 = array(
'post_type' => 'products', //custom post type
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'brands',
'field' => 'slug',
'terms' => $search,
),
),
'posts_per_page' => -1,
);
$merged = array_merge($args, $args2);
$search_query = new WP_Query($merged);
if ($search_query->have_posts()) :
And so on with the loop; So what am I missing with the merge arguments?
Essentially i am using 's' => $search only in one of the arrays to search the post name, which I do not need (or believe i need) in the brands taxonomy array
You can combine queries like this. You can add multiple tax_query.
$search = sanitize_text_field($_POST['productsearch']);
$post_category = $_POST['post_cat'];
$args2 = array(
'post_type' => 'products', //custom post type
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
's' => $search,
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'brands',
'field' => 'slug',
'terms' => $search
),
array(
'taxonomy' => 'productscat',
'field' => 'id',
'terms' => $post_category
)
)
);
$search_query = new WP_Query( $args2 );
if ( $search_query->have_posts() ) :
endif;
I get product by:
$args = array(
'post_type' => 'product',
'posts_per_page' => 15,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $theCat
)
)
);
$post= new WP_Query( $args );
I want to get products but order by view so I did:
$args = array(
'post_type' => 'product',
'posts_per_page' => 15,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $theCat
)
),
'order' => 'ASC', // add this
'suppress_filters' => false, // add this
'orderby' => 'post_views' // add this
);
$post = new WP_Query( $args );
But still show the same result, any idea?
Not sure if this helps but instead of 'ASC' try 'DESC'?
I tried many methods, but I can't show products with category name or id
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'cat' => 40,
'posts_per_page' => '12',
);
$loop = new WP_Query( $args );
Try this example,
So given a category with ID 26, the following code would return it's products (WooCommerce 3+):
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => 26,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too
'operator' => 'NOT IN'
)
)
);
$products = new WP_Query($args);
var_dump($products);
Try tax_query
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array('40'),
'operator' => 'IN',
)
)
);
$loop = new WP_Query( $args );
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Try this,
<?php
$args = array( 'post_type' => 'product', 'post_status' => 'publish','category' => 34, 'ignore_sticky_posts' => 1, 'posts_per_page' => '12',);
$products = get_posts( $args );
?>
Hope this will helps you. for more info.
I suggest used >> wc_get_product_terms( $vars->ID, 'pa_brand_category' )
please check this screenshot >> http://prntscr.com/hjawu5
I want to display all products from multiple categories at once.
When i want to display all products from one category my $args array looks like this:
$args = array(
'post_type' => 'product',
'product_cat' => 'backpacks',
'orderby' => '_sku'
);
I remember that I can simply make an array inside my $args:
$args = array(
'post_type' => 'product',
'product_cat' => array(
'backpacks','accessoires',
),
'orderby' => '_sku'
);
But it gives me the following error:
Warning: urlencode() expects parameter 1 to be string, array given in C:\xampp\htdocs\live\wp-includes\formatting.php on line 4312
I know this is a simple thing but i cant figure out why its not working.
Thanks for any help!
Please try below snippet.
$sortcolumn = 'ID';
$prod_categories = array(12, 17); //category IDs
$product_args = array(
'numberposts' => -1,
'post_status' => array('publish', 'pending', 'private', 'draft'),
'post_type' => array('product', 'product_variation'), //skip types
'orderby' => $sortcolumn,
'order' => 'ASC',
);
if (!empty($prod_categories)) {
$product_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $prod_categories,
'operator' => 'IN',
));
}
$products = get_posts($product_args);
Found a simple way to do it
$args = array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'backpacks'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'accessoires'
)
),
);