30. Tìm hiểu kỹ hơn về change_query (ok)

C:\xampp\htdocs\test\wp-content\plugins\plugin-name\public\class-wpf-public_ct.php

Không ghi đè var trống và có giá trị mặc định

public function change_query( $query ) {
  if ( $form = $this->get_form( sanitize_key( $_GET['wpf_ct'] ) ) ) {
    $args = $this->parse_query( $_GET, $form );
    echo '<pre>';
    foreach ( $args as $k => $v ) {
      // Don't override the var that is empty and has default value #8913
      if(empty($args[$k]) && !empty($query->get($k))){
        continue;
      }
      $query->set( $k, $v );
    }
    echo '</pre>';
  }
}

1.

echo '<pre>';
    var_export($args);
echo '</pre>';
array (
  'post_type' => 'product',
  'post_status' => 'publish',
  'is_paginated' => 1,
  'meta_query' => 
  array (
  ),
  'tax_query' => 
  array (
    0 => 
    array (
      'taxonomy' => 'product_cat',
      'field' => 'slug',
      'terms' => 
      array (
        0 => 'decor',
      ),
      'operator' => 'IN',
      'include_children' => true,
    ),
    'relation' => 'or',
  ),
  'post__not_in' => 
  array (
  ),
  'posts_per_page' => '10',
  'paged' => 1,
  'offset' => 0,
  'orderby' => 'menu_order title',
  'order' => 'asc',
)

2.

echo '<pre>';
    var_export($k);
echo '</pre>';
<pre>
  <pre>'post_type'</pre>
  <pre>'post_status'</pre>
  <pre>'is_paginated'</pre>
  <pre>'meta_query'</pre>
  <pre>'tax_query'</pre>
  <pre>'post__not_in'</pre>
  <pre>'posts_per_page'</pre>
  <pre>'paged'</pre>
  <pre>'offset'</pre>
  <pre>'orderby'</pre>
  <pre>'order'</pre>
</pre>

3.

echo '<pre>';
    var_export($query->get($k));
echo '</pre>';
<pre>
  <pre>'product'</pre>
  <pre>''</pre>
  <pre>''</pre>
  <pre>
  array (
  )
  </pre>
  <pre>
  array (
    'relation' => 'AND',
    0 => 
    array (
      'taxonomy' => 'product_visibility',
      'field' => 'term_taxonomy_id',
      'terms' => 
      array (
        0 => 7,
      ),
      'operator' => 'NOT IN',
    ),
  )
  </pre>
  <pre>
  array (
  )
  </pre>
  <pre>16</pre>
  <pre>0</pre>
  <pre>''</pre>
  <pre>'menu_order title'</pre>
  <pre>'ASC'</pre>
</pre>

Last updated