Let users sort messages in Wordpress

I like to create a page to filter messages by several criteria.

I can work with wp_query and deliver messages quite easily, my problem is that I can not understand (and can not find any answers on the Internet about this, believe me, I looked) how to allow users to do this.

Take this, for example, returning messages in order of price (custom meta value field) from highest to lowest with 33 messages.

 <?php $featuredPosts = new WP_Query( array( 'posts_per_page' => 33, 'meta_key'=>'Price', 'orderby' => 'meta_value_num', 'order' => DESC ) ); ?> <?php if ( $featuredPosts->have_posts() ) : ?> <?php while ( $featuredPosts->have_posts() ) : $featuredPosts->the_post(); ?> <article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>"> <h2 class="price-title"><?php the_title(); ?> </h2> </article> <!-- end div post --> <?php endwhile; wp_reset_query(); ?> <?php endif; ?> 

Now, even after reading and searching on Google, I will be damned if I can understand how Id implements this at the front end so that users can filter messages.

I mean, I know that you can add URLs in Wordpress to change the order of posts, but in this context I completely lost.

I tried this, but it does not work.

 <?php $by_price = esc_url(add_query_arg(array( 'meta_key' => 'price', 'orderby' => 'meta_value_num', 'order' => ASC ))); $by_date = esc_url(add_query_arg(array( 'orderby' => 'date', 'order' => DESC ))); ?> <ul> <li><a href="<?php echo $by_price;?>">Order by price</a></li> <li><a href="<?php echo $by_date;?>">Order by date</a></li> </ul> 

What I'm trying to achieve is actually quite simple, let the user select a category, select a price range (guessing that Id is writing something in jQuery to deliver a value to the field), set the number of results that they would like to use.

Ive tried to pick everything up under the sun, which I can think of, without cubes.

+6
source share
3 answers

Try the Simple Custom Email Order Plugin .

It uses AJAX and JavaScript, you do not need to download anything. You just need to drag and drop messages.

+3
source

OK, I am updating the code to make it clear:

--- I do not think meta_key will automatically fire ---

functions.php

 ... $whitList = array( 'price' => array( 'posts_per_page' => 33, 'meta_key'=>'price', 'orderby'=>'meta_value_num', 'order' => ASC ), 'date' => array( 'posts_per_page' => 33, 'orderby'=>'date', 'order' => DESC ) ); ... 

Your first php loop:

 <?php gloabl $whitList; //to use the $whitList in your functions.php. $aryQuery = $whitList[$_REQUEST['orderby']] ? $whitList[$_REQUEST['orderby']] : $whitList['price']; $featuredPosts = new WP_Query( $aryQuery ); .... .... ?> 

For your list page:

 <ul> <?php gloabl $whitList; //to use the $whitList in your functions.php. foreach( $whitList as $orderby => $aryOrderBySettings){ ?> <li> <a href="<?php echo esc_url(add_query_arg('orderby', $orderby));?>">Order by <?php echo $orderby;?></a></li> <?php } ?> </ul> 
0
source

Using the $_GET parameters is the way to go. First of all, you want your visitors to access them, add these variables. In general, the link approach is very good, so we can generate additional links using add_query_arg to use additional parameters for the current URL.

 <?php $urla = add_query_arg( 'sort' => 'price', 'asc' => '1' ); $urld = add_query_arg( 'sort' => 'price', 'asc' => '0' ); ?> <a href="<?php echo esc_url( $url ); ?>">Sort by price (asc)</a> <a href="<?php echo esc_url( $url ); ?>">Sort by price (desc)</a> 

When clicked, this way, you can find the variables bound to:

 <?php // Get an allowed sort variable and the order $sort = isset( $_GET['sort'] ) && in_array( $_GET['sort'], array( 'price' ) ) ) ? $_GET['sort'] : null; $order = isset( $_GET['asc'] ) && $_GET['asc'] == '0' ? 'DESC' : 'ASC'; ?> 

Now you should increase your main query with the data you just received. If you use the standard method for query_posts messages on a page, you can leave with query_posts , although this is not recommended . And, if you are using a custom loop, just insert the new arguments into it:

 <?php $args = array(); switch ( $sort ): case 'price': $args['order'] = $order; $args['orderby'] = 'meta_value_num'; $args['meta_key'] = 'price'; break; default: break; endswitch; $defaults = array( 'posts_per_page' => 33 ); $query = new WP_Query( wp_parse_args( $args, $defaults ) ); ?> 

You can add more variables by creating more URLs and buttons to click, and even more cases in the switch statement to extend the basic example above.

The first code snippet will be displayed wherever you want your buttons to appear. The second piece of code goes to the third, which precedes the output of the results.

0
source

All Articles