Iâd like to create a page for filtering posts based on a number of criteria.
I can work with wp_query
and deliver posts quite easily, my problem is that I canât figure out (nor can I find any answers online about this, believe me I looked) how to let users do this.
Take this for example, returns the posts in order of price (custom field meta value) from highest to lowest with 33 posts.
<?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 googling, Iâll be damned if I can figure out how Iâd implement this on the front end for users to filter posts.
I mean, I know you can append to the URLs in WordPress to alter the order of posts, but in this context Iâm totally lost.
I tried this, but it doesn’t 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 as well, let the user choose the category, choose the price range (guessing Iâd write something in JQuery to deliver a value into an field), set the number of results theyâd like to be returned.
Iâve tried googling everything under the sun I can think of for this, no dice.
Try Simple Custom Post Order plugin.
It is using AJAX and JavaScript, you donât have to load anything else. You have to just drag and drop the posts.
OK, I update the code to make it clear:
—I do not think meta_key would be auto-pickup—
functions.php
Your first loop php:
For your list page:
Using
$_GET
parameters is the way to go here. First of all you’ll want to allow your visitors access to these add these variables. The link approach is fine, overall, so we can generate augmented links by using theadd_query_arg
to tack on extra parameters to the current URL.When clicked, the tacked on variables can thus be detected:
Now you would augment your main query with the data you just retrieved. If you’re using the default way of querying posts on a page you should be able to get away with
query_posts
, although it is not recomended. And, if you’re using a custom loop, simply inject the new arguments into it:You can add more variables, by creating more URLs and buttons to press, and more cases in the switch statement to extend the basic example above.
The first piece of code would go wherever you want your buttons to appear. The second piece of code goes before the third one, which goes before outputting the results.