【问题标题】:WP query and multiple keyWP查询和多键
【发布时间】:2023-04-02 06:58:01
【问题描述】:

我正在尝试在我的 wordpress 中实现多个元键过滤器。这很简单,我可以通过表单来过滤我的帖子。如果我只用完美的“价格”查询来实现这个。如果我添加“流派”不起作用,则查询不起作用。

对于字段“流派”,我使用高级自定义字段中的复选框,结构为“homme : Homme / femme : Femme”。

我测试了不同的东西,比如删除“价格”和查询“类型”也不起作用......

我从中获得价值

  <?php
     if($_GET['minprice'] && !empty($_GET['minprice']))
        {
            $minprice = $_GET['minprice'];
        } else {
            $minprice = 0;
        }

        if($_GET['maxprice'] && !empty($_GET['maxprice']))
        {
            $maxprice = $_GET['maxprice'];
        } else {
            $maxprice = 1000;
        }

 if($_GET['genre'] && !empty($_GET['genre']))
        {
            $genre = $_GET['genre'];
        }
    ?>

我的查询看起来像这样

 $args = array(
        'cat' => $cat,
        'post_type' => 'post',
        'posts_per_page' => 28,
        'paged' => $paged,
        'meta_query' => array(
            'relation' => 'AND',

                array(
                    'key' => 'prix',
                    'type' => 'NUMERIC',
                    'value' => array($minprice, $maxprice),
                    'compare' => 'BETWEEN'
                    ), 

                array(
                    'key' => 'genre',
                    'value' => $genre,
                    'compare' => 'LIKE'
                    )
                )
            );

我的查询循环

    <?php
  // set up or arguments for our custom query
  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  $args = array(
    'cat' => $cat,
    'post_type' => 'post',
    'posts_per_page' => 28,
    'paged' => $paged,
    'meta_query' => array(
        'relation' => 'AND',

            array(
                'key' => 'prix',
                'type' => 'NUMERIC',
                'value' => array($minprice, $maxprice),
                'compare' => 'BETWEEN'
                ), 

            array(
                'key' => 'genre',
                'value' => $genre,
                'compare' => 'LIKE'
                )
            )
        );

// create a new instance of WP_Query
  $the_query = new WP_Query($args);
?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>

  <?php
 get_template_part( 'content-category', get_post_format() );
 ?>


<?php endwhile; ?>

<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
  <div class="clearfix"></div>

<?php bootstrap_pagination();?>

<?php } ?>

<?php else: ?>
        <?php get_template_part( 'no-results', 'archive' ); ?>
  <?php endif; ?>
</div>  
<?php wp_reset_query(); ?>

我对此进行了测试,它的工作原理!

 $args = array(
            'cat' => $cat,
            'post_type' => 'post',
            'posts_per_page' => 28,
            'paged' => $paged,
            'meta_query' => array(
                    array(
                        'key' => 'prix',
                        'type' => 'NUMERIC',
                        'value' => array($minprice, $maxprice),
                        'compare' => 'BETWEEN'
                        ), 

                    )
                );

但这不起作用

$args = array(
            'cat' => $cat,
            'post_type' => 'post',
            'posts_per_page' => 28,
            'paged' => $paged,
            'meta_query' => array(         
                array(
                        'key' => 'genre',
                        'value' => $genre,
                        'compare' => 'LIKE'
                        )
                    )
                );

拜托,你能帮我吗,因为我正在失去理智...... 谢谢!

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    我认为您在 meta_query 中缺少一个包装数组

    $args = array(
       'cat'            => $cat,
       'post_type'      => 'post',
       'posts_per_page' => 28,
       'paged'          => $paged,
       'meta_query'     => array(
          array(
             'relation' => 'AND',
             array(
                'key'     => 'prix',
                'type'    => 'NUMERIC',
                'value'   => array( $minprice, $maxprice ),
                'compare' => 'BETWEEN'
             ), 
             array(
                'key'     => 'genre',
                'value'   => $genre,
                'compare' => 'LIKE'
             ),
          ),
       ),
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-14
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      相关资源
      最近更新 更多