0

I have some code here that when you click on certain links it reloads the page and sorts my posts based on the query array that I have set. I am just wondering how do I make it load and sort by ajax instead of reloading the page?

Here is the code I am using:

 <?php 
    $sort= esc_attr($_GET['sort']); 
    if($sort == "title_asc") { $order= "&orderby=title&order=ASC"; } 
    if($sort == "title_desc") { $order= "&orderby=title&order=DESC"; }
    if($sort == "date_desc") { $order= "&orderby=date&order=DESC"; }
    if($sort == "date_asc") { $order= "&orderby=date&order=ASC"; }
    ?>

            <div class="elementsToFilter">
              <ul>
<li><a  href="?sort=title_asc" <?php if ($sort == "title_asc"){ echo 'style="color:gray"'; } ?>>Title : A - Z</a></li>
<li><a href="?sort=title_desc" <?php if ($sort == "title_desc"){ echo 'style="color:gray"'; } ?>>Title : Z - A</a></li>
<li><a href="?sort=date_desc" <?php if ($sort == "date_desc"){ echo 'style="color:gray"'; } ?>>Date : Newest - Oldest</a></li>
<li><a href="?sort=date_asc" <?php if ($sort == "date_asc"){ echo 'style="color:gray"'; } ?>>Date : Oldest - Newest</a></li>
              </ul>
            </div>

<?php $the_query = new WP_Query( 'cat=3309&posts_per_page=12'. $order); ?>
    <?php while($the_query->have_posts()): $the_query->the_post(); ?>
                        <?php if($view_type == 'grid'): ?>
                            <?php get_template_part('partials/blog/content-grid', 'loop'); ?>
                        <?php else: ?>
                            <?php get_template_part('partials/blog/content-list', 'loop'); ?>
                        <?php endif; ?>
                    <?php endwhile; ?>
0

I am by no means an AJAX expert, I have only ever used for pretty much what you are needing it for. It can be quite a lengthy explanation for a forum post. But when I was trying to achieve the same thing the first time, I found this excellent YouTube video series (5 parts) by Brandon Brotsky on how to filter custom post types using AJAX. Even if your only filtering regular posts and not using ACF, you should be able to use his method to filter posts by date or title in ASC or DESC order. Actually it should be easier than what he is doing in his video.

https://www.youtube.com/watch?v=e5tUkHczcVE

I know it's probably not the answer you were hoping for, but I think you will find his video helpful to some extent.

  • Thank you very much for the reply and for providing this information, I really appreciate it. I will have a look now and hopefully I can get some results. – Roger Alridge Oct 6 '17 at 21:39
  • Hi Roger, just wanted to check in and see if that video series was of any help? If you have any specific questions feel free to ask. – Jonathan Ryan Patterson Oct 11 '17 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.