using a ACF field for terms in params array - php

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'
)
)
);

Related

Combine two or more OR relations in WP Query? [duplicate]

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;

display two woocommerce categories on front page

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'
)
),
)
);
?>

Combine two separate wp queries into one single query for search

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;

Get all products from multiple categories (Woocommerce/Wordpress)

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'
)
),
);

WP query if in category OR has taxonomy

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 );

Categories