Answer the question
In order to leave comments, you need to log in
How to add meta fields to the request of the main output loop in woocommerce/archive-product.php?
I created the 'New' meta field for the 'product_cat' taxonomy terms using the ACF plugin and I want to select all terms with a marked meta field in a cycle.
I added (as an example) code to the archive-product.php file to check how the request for the main post loop changes:
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'new_product', // name of custom field
'value' => true,
'type' => 'numeric',
'compare' => 'LIKE'
)
)
);
global $wp_query;
$args = array_merge( $wp_query->query, $args );
query_posts($args);
if ( woocommerce_product_loop() ) {
// родной код вывода термов из content-product
}
["product_cat"]=> string(10) "wallpapers" //первоначальная строка запроса
["meta_query"]=> array(1) {
[0]=> array(4) {
["key"]=> string(11) "new_product" ["value"]=> int(1) ["type"]=> string(7) "numeric" ["compare"]=> string(4) "LIKE"
}
}
function custom_posts_join($join){
global $wpdb;
$join .= " LEFT JOIN $wpdb->termmeta as meta_1 ON $wpdb->terms.term_id = meta_1.term_id";
return $join;
}
add_filter( 'posts_join' , 'custom_posts_join');
function filter_where( $where = '' ) {
$where .= " AND meta_1.meta_key='new_product' AND meta_1.meta_value=1";
}
add_filter('posts_where', 'filter_where');
Answer the question
In order to leave comments, you need to log in
Solution found!!! If it's useful to anyone, I found a hook to override the request before displaying categories:
add_filter( 'woocommerce_product_subcategories_args', 'update_subcategories_args_func',1 );
function update_subcategories_args_func($query) {
if ($_REQUEST['new_product'] == '1') {
$args = array(
'meta_query' => array(
array(
'key' => 'new_product', // name of custom field
'value' => 1,
'type' => 'numeric',
// 'compare' => 'LIKE'
)
)
);
return wp_parse_args( $args, $query );
} else return $query;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question