S
S
Stanislav Shabalin2020-03-01 00:12:49
SQL
Stanislav Shabalin, 2020-03-01 00:12:49

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
}


Here are the new request arguments:
["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" 
  } 
}


But after executing query_posts() nothing happens, the terms of the previous $wp_query query are displayed.

There is an option to form the query differently, like this, but I'm not sure that it will be suitable for selecting terms:

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

But I don't know how to apply it when displaying records in the archive-product.php template.

I must say right away that I used the Premmerce Woocommerce Product Filter plugin as the main category filter in the store.

Tell me, is it possible to change the main loop and the filter widget while also working correctly? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Shabalin, 2020-03-04
@Starck43

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 question

Ask a Question

731 491 924 answers to any question