I have two categories called mobile and laptop, which I want to show both at the same time on the main page. I used this code but it only shows one of the categories. Thank you for your help
<?php $products= new WP_Query(
array(
'post_type'=>'product',
'posts_per_page'=>'-1',
'product_cat' => 'mobile+laptop',
'orderby' => 'rand',
)
); ?>
You can use tax_query. try the below code.
<?php
$products = new WP_Query(
array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'rand',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'mobile'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'laptop'
)
),
)
);
?>
Related
I am trying to pass the category id from an ACF field to the params array in a custom product template, but I just get the number echoed on to the page, rather than the actual product feed.
`<?php
$params = array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => the_field('cat_id'),
'operator' => 'IN'
)
)
);
$wc_query = new WP_Query($params);
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) :
$wc_query->the_post();
?>`
use get_field() function:
$params = array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => get_field('cat_id'),
'operator' => 'IN'
)
)
);
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'?
Can you help me with this? I have a product list and what I want is to hide those products marked as hidden on the visibility detail of the product. Here's the code I have:
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
// 'terms' => 'white-wines'
'terms' => $product_categories[$wc_arr_key[$wc_cat_id]]->term_id,
'visibility' => 'visible' //NOT WORKING...
)
),
'post_type' => 'product',
'orderby' => 'menu_order',
'order' => 'ASC',
);
$products = new WP_Query( $args );
if(isset($_GET['staging']) && $_GET['staging'] == "true") {
echo "<pre>" . print_r($products) . "</pre>";
}
I want to display all the products that are marked as visible.
Since Woocommerce 3 the product visibility is handled by the taxonomy product_visibility for the term exclude-from-catalog, so you need to use a second array in your tax query array:
$terms = array( $product_categories[$wc_arr_key[$wc_cat_id]]->term_id );
$products = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $terms
),
array(
'taxonomy' => 'product_visibility',
'terms' => array('exclude-from-catalog'),
'field' => 'name',
'operator' => 'NOT IN',
),
),
'orderby' => 'menu_order',
'order' => 'ASC',
) );
if(isset($_GET['staging']) && $_GET['staging'] == "true") {
echo "<pre>" . print_r($products) . "</pre>";
}
Tested and works.
Related: Database changes for products in woocommerce 3
Use below code to exclude hidden products and display only visible ones
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
// 'terms' => 'white-wines'
'terms' => $product_categories[$wc_arr_key[$wc_cat_id]]->term_id
)
),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => '!='
)
),
'post_type' => 'product',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$products = new WP_Query( $args );
if(isset($_GET['staging']) && $_GET['staging'] == "true") {
echo "<pre>" . print_r($products) . "</pre>";
}
WooCommerce save this data as metadata so you'll need to run a Meta Query against the name _visibility. This will look like
'meta_query' => array(
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => '!=',
)
)
This will pull all posts that do not have meta _visibility equal to hidden
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'
)
),
);
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 );