D
D
Danil Trubitsyn2021-11-10 09:30:38
WordPress
Danil Trubitsyn, 2021-11-10 09:30:38

How to pass variable value to wordpress attachable function?

You need to pass $product_ids to the repeater_dynamic_query() function.

// The query
$products = new WP_Query( array(
  'post_type'      => array('product'),
  'post_status'    => 'publish',
  'posts_per_page' => -1,
  'tax_query'      => array( 
    array(
      'taxonomy'        => 'pa_aktsiya',
      'field'           => 'slug',
      'terms'           =>  array('spetspredlozheniya'),
      'operator'        => 'IN',
    ),
    array(
      'taxonomy'  => 'product_visibility',
      'terms'     => array( 'exclude-from-catalog' ),
      'field'     => 'name',
      'operator'  => 'NOT IN',
    ) )
) );

// The Loop
if ( $products->have_posts() ): while ( $products->have_posts() ):
$products->the_post();
$product_ids[] = $products->post->ID;
endwhile;
wp_reset_postdata();
endif;

function repeater_dynamic_query( $query ) {

  if ( $query->query['post_type'][0] == 'product' ) {


    $query->set( 'post__in', $product_ids );
    $query->set( 'no_found_rows', true );
  }
}

add_action( 'pre_get_posts', 'repeater_dynamic_query' );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-11-10
@ali3412

add_action( 'pre_get_posts', 'repeater_dynamic_query' );
function repeater_dynamic_query( $query ) {

  if ( $query->get('post_type') === 'product' ) {

    $tax_query['tax_query'] = array( 
      array(
        'taxonomy' => 'pa_aktsiya',
        'field' => 'slug',
        'terms' =>  array( 'spetspredlozheniya' ),
        'operator' => 'IN',
      ),
      array(
        'taxonomy' => 'product_visibility',
        'field' => 'name',
        'terms' => array( 'exclude-from-catalog' ),
        'operator' => 'NOT IN',
      ) 
    );

    $query->set( 'tax_query', $tax_query );
    $query->set( 'no_found_rows', true );
    
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question