使用URL查询变量更改WP_QUERY参数

时间:2019-03-13 作者:Pr0metheus

:)

我已经使用WP\\u Query在此处显示了这些帖子(旅行)http://boost19.delavec.si/wordpressP/produkti/

所以我有一个WP\\U查询,它在我的自定义帖子中循环

  <?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; ?>
            <?php $loop = new WP_Query( array(
                \'post_type\' => \'product\',
                \'meta_key\' => \'product_price\',
                \'orderby\' => \'meta_value_num\',
                \'order\' => \'ASC\',
                \'posts_per_page\' => 4,
                \'paged\' => $paged) ); ?>

            <?php while( $loop->have_posts() ) : $loop->the_post(); ?>

            <div class="av_one_third" style="display: inline-block; width: 20%; margin-bottom: 3%; text-align: center; margin-left: 3%;">
                <div class="card rounded-0 bg-light mb-3" style="width: 225px; height: 325px; text-align: center">
                    <h5 class="card-title"><?php the_title(); ?></h5>
                    <a href="<?php the_permalink(); ?>">
                        <?php the_post_thumbnail(); ?>
                    </a>
                    <p>We are going to <?php the_field(\'product_place\'); ?></p>
                    <p>Product price: <?php the_field(\'product_price\'); ?>€</p>
                    <?php if(get_field(\'product_discount_price\')): ?>
                        <h4>Discount: <?php the_field(\'product_discount_price\'); ?>€</h4>
                    <?php endif; ?>
                    <p>We are going on <?php the_field(\'product_date\'); ?></p>
                </div>
            </div>
        <?php endwhile; ?>
我希望添加类似“排序”的操作-我想知道是否可以将“order”更改为“DESC”作为初学者?

我试过读不同的东西,比如https://premium.wpmudev.org/blog/building-customized-urls-wordpress/https://codex.wordpress.org/WordPress_Query_Vars

并尝试了这一点,但没有结果;

http://boost19.delavec.si/wordpressP/produkti/?order=DESChttp://boost19.delavec.si/wordpressP/?post_type=product&orderby=meta_value_num&meta_key=product_price&order=DESC

它不会返回应该返回的真值。

有人面对过这样的事情吗?或者,如果有人有任何其他想法,如何通过价格解决这类问题?

顺便说一句,我使用的是高级自定义字段-价格字段的名称是

产品价格

1 个回复
SO网友:admcfajn

此代码手动设置订单\'order\' => \'ASC\' WP\\u查询参数中的声明。

$loop = new WP_Query( array(
  \'post_type\' => \'product\',
  \'meta_key\' => \'product_price\',
  \'orderby\' => \'meta_value_num\',
  \'order\' => \'ASC\',
  \'posts_per_page\' => 4,
  \'paged\' => $paged) );
如果要将url参数传递给该参数,可以使用如下内容:

\'order\' => $_GET[\'posts_order\']

以及url模式,如:/?posts_order=ASC

但那将是一个可怕的想法。。。因为有人可以访问url:/?posts_order=DROP TABLE IF EXISTS table1 &;可能会破坏事物。

因此,如果我们要侦听url中的变量,我们需要对其进行清理。

<?php 
if ( ! empty( $_GET[\'posts_order\'] ) ) {
  $posts_order = sanitize_key( $_GET[\'posts_order\'] );  // prevent malicious characters in user-submitted variable
}
或者,更好的是,我们可以使用白名单方法&;仅允许的某些值

$posts_order = \'DESC\';
if ( ! empty( $_GET[\'posts_order\'] ) ) {
  $posts_order_raw = sanitize_key( $_GET[\'posts_order\'] );
  if ( \'ASC\' === $posts_order_raw ) {
    $posts_order = \'ASC\';
  }
}

$loop = new WP_Query( array(
  \'post_type\' => \'product\',
  \'meta_key\' => \'product_price\',
  \'orderby\' => \'meta_value_num\',
  \'order\' => $posts_order,
  \'posts_per_page\' => 4,
  \'paged\' => $paged) ); 

但是等等,还有更多!

我们可以从WP\\u查询中完全省略订单声明

$loop = new WP_Query( array(
  \'post_type\' => \'product\',
  \'meta_key\' => \'product_price\',
  \'orderby\' => \'meta_value_num\',
  // \'order\' => $posts_order, # Remove this line #
  \'posts_per_page\' => 4,
  \'paged\' => $paged) );
&;相反,从pre_get_posts 挂钩:

<?php
function wp_se_331547( $wp_query ) {

  if ( !is_admin() && is_main_query() ) {
    if( \'product\' === get_post_type() ) {
      $posts_order = \'DESC\';
      if ( ! empty( $_GET[\'posts_order\'] ) ) {
        $posts_order_raw = sanitize_key( $_GET[\'posts_order\'] );
        if ( \'ASC\' === $posts_order_raw ) {
          $posts_order = \'ASC\';
        }
      }
      $wp_query->set( \'order\', $posts_order );
    }
  }

}
add_action(\'pre_get_posts\', \'wp_se_331547\');

相关推荐

如何在class-wp-query.php中跟踪核心函数is_page()&is_Single上的通知警告

在我的生产站点上,我的这些产品已经超支了:PHP Notice: Trying to get property of non-object in /public_html/wp-includes/class-wp-query.php on line 3728 PHP Notice: Trying to get property of non-object in /public_html/wp-includes/class-wp-query.php on line 3730 PHP No