最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Changing WP_Query params with url Query Var

programmeradmin3浏览0评论

:)

I've used WP_Query to display there posts (travels) here /

So... I've got this WP_Query that is looping through my custom posts

  <?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; ?>

And I wish to add like 'sorting' action to it - I was wondering if I could change the 'order' to 'DESC' for starters?

I've tried and read different things like /

and tried this with no avail ;

/?order=DESC or /?post_type=product&orderby=meta_value_num&meta_key=product_price&order=DESC

which doesn't return true values that is should.

Anyone faced anything like that? Or if anyone has any other ideas how to tacke this sort by price thing?

Btw I'm using advanced custom fields - the name for the price field is

product_price

:)

I've used WP_Query to display there posts (travels) here http://boost19.delavec.si/wordpressP/produkti/

So... I've got this WP_Query that is looping through my custom posts

  <?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; ?>

And I wish to add like 'sorting' action to it - I was wondering if I could change the 'order' to 'DESC' for starters?

I've tried and read different things like https://premium.wpmudev.org/blog/building-customized-urls-wordpress/ https://codex.wordpress.org/WordPress_Query_Vars

and tried this with no avail ;

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

which doesn't return true values that is should.

Anyone faced anything like that? Or if anyone has any other ideas how to tacke this sort by price thing?

Btw I'm using advanced custom fields - the name for the price field is

product_price

Share Improve this question asked Mar 13, 2019 at 15:30 Pr0metheusPr0metheus 232 silver badges8 bronze badges 2
  • Is there a particular reason this is a custom query and not the main query? Post type archives already query the correct posts, and you can use query vars to do what you want. – Jacob Peattie Commented Mar 14, 2019 at 8:54
  • Hmmm could you elaborate a little in more detail how you'd tackle it? I'd be really grateful if you have the time? I have to do the same thing (sort posts) for custom taxonomy pages like : boost19.delavec.si/wordpressP/product_locations/asia where I need to have custom query to draw posts from specific category – Pr0metheus Commented Mar 15, 2019 at 9:26
Add a comment  | 

1 Answer 1

Reset to default 1

This code manually sets the order with the 'order' => 'ASC' declaration in the WP_Query arguments.

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

If we want to pass a url parameter to that we could use something like:

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

along with a url-pattern, like: /?posts_order=ASC

But that would be a terrible idea... because someone could visit the url: /?posts_order=DROP TABLE IF EXISTS table1 & potentially break things.

So, if we're going to listen for a variable in the url, we'll want to sanitize it.

<?php 
if ( ! empty( $_GET['posts_order'] ) ) {
  $posts_order = sanitize_key( $_GET['posts_order'] );  // prevent malicious characters in user-submitted variable
}

or, even better, we can use a whitelist-approach & only allow certain values for

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

But wait, there's more!

We could omit the order declaration entirely from the WP_Query

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

& instead, control the order from the pre_get_posts hook:

<?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');
发布评论

评论列表(0)

  1. 暂无评论