Search code examples
phpwordpressloopstagsarchive

Load more in archive based on tag


I am working on a load more button that loads posts in the archive.php based on tags. I have gotten the load more button to work on a custom post type and regular posts using this example, https://artisansweb.net/load-wordpress-post-ajax/. The archive page is little different because this loop is creating itself based on slug. Here is my current working loop on the archive.php

<?php
 $term_slug = get_queried_object()->slug;

 $loop = new WP_Query( array( 'post_type' => '', 'tag' => 
 $term_slug, 'posts_per_page' => 3 ) ); ?>
 <ul class="my-archive">
 <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

  <li class="blog-box">
        <div class="blog-bullet-img">
      <a href="<?php the_permalink(); ?>" title="<?php 
      the_title_attribute(); ?>">
        <?php the_post_thumbnail(); ?>
      </a>
      </div>
      <div class="news-full-descrip">
          <p class="blog_title"><?php the_title(); ?></p>
      <p class="blog_date"><?php echo get_the_date(); ?></p>
      <p class="blog-textarea"><?php echo get_the_excerpt(); ?></p>
      </div>
    </li>

 <?php endwhile; // End the loop. ?> 

 </ul>

 <div class="loadmore_three"><span>Load More</span></div>
 </div>

 <script type="text/javascript">
 var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
 var page = 2;
 jQuery(function($) {
 $('body').on('click', '.loadmore_three', function() {
    var data = {
        'action': 'load_posts_by_ajax_three',
        'page': page,
        'security_three': '<?php echo 
 wp_create_nonce("load_more_posts_three"); ?>'
    };

    $.post(ajaxurl, data, function(response) {
        $('.my-archive').append(response);
        page++;
    });
});
});
</script>  

In the functions.php I have added a slightly modified version of this loop adding $paged so it coincides with the example in my above link. This currently returns all posts and does not filter the tag by slug when you click the load more.

// The Archive post type load more function
add_action('wp_ajax_load_posts_by_ajax_three', 
'load_posts_by_ajax_callback_three');
add_action('wp_ajax_nopriv_load_posts_by_ajax_three', 
'load_posts_by_ajax_callback_three');

function load_posts_by_ajax_callback_three() {
check_ajax_referer('load_more_posts_three', 'security_three');      


$term_slug = get_queried_object()->slug;
$paged = $_POST['page'];
$loop = new WP_Query( array( 'post_type' => '', 'tag' => $term_slug, 
'posts_per_page' => 3, 'paged' => $paged, ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<li class="blog-box">
        <div class="blog-bullet-img">
      <a href="<?php the_permalink(); ?>" title="<?php 
the_title_attribute(); ?>">
        <?php the_post_thumbnail(); ?>
      </a>
      </div>
      <div class="news-full-descrip">
          <p class="blog_title"><?php the_title(); ?></p>
      <p class="blog_date"><?php echo get_the_date(); ?></p>
      <p class="blog-textarea"><?php echo get_the_excerpt(); ?></p>
      </div>
    </li>

<?php endwhile;  wp_die(); }     

I'm not great at PHP but have been reading a lot and trying to understand it better. When you click load more I would like this to return the posts with the same tag as the current url or slug.


Solution

  • You have to pass your slug term into your AJAX , you can do something like this:

    $term_slug = get_queried_object()->slug;
    
    $loop = new WP_Query( array( 'post_type' => '', 'tag' =>
    $term_slug, 'posts_per_page' => 1 ) ); ?>
    <ul class="my-archive">
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
            <li class="blog-box">
                <div class="blog-bullet-img">
                    <a href="<?php the_permalink(); ?>" title="<?php
                    the_title_attribute(); ?>">
                        <?php the_post_thumbnail(); ?>
                    </a>
                </div>
                <div class="news-full-descrip">
                    <p class="blog_title"><?php the_title(); ?></p>
                    <p class="blog_date"><?php echo get_the_date(); ?></p>
                    <p class="blog-textarea"><?php echo get_the_excerpt(); ?></p>
                </div>
            </li>
    
        <?php endwhile; // End the loop. ?>
    
    </ul>
    
    <div class="loadmore_three"><span>Load More</span></div>
    </div>
    
    <?php get_footer(); ?>
    
    <script type="text/javascript">
        var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
    
        jQuery(function($) {
            var page = 2;
            $('body').on('click', '.loadmore_three', function() {
                var data = {
                    'action': 'load_posts_by_ajax_three',
                    'page': page,
                    'slugTerm' : '<?php echo $term_slug; ?>',
                    'security_three': '<?php echo
                    wp_create_nonce("load_more_posts_three"); ?>'
                };
    
                $.post(ajaxurl, data, function(response) {
                    $('.my-archive').append(response);
                    page++;
                });
            });
        });
    </script>
    

    In your functions.php :

    add_action('wp_ajax_load_posts_by_ajax_three',
        'load_posts_by_ajax_callback_three');
    add_action('wp_ajax_nopriv_load_posts_by_ajax_three',
        'load_posts_by_ajax_callback_three');
    
    function load_posts_by_ajax_callback_three() {
        check_ajax_referer('load_more_posts_three', 'security_three');
    
    
        $term_slug= $_POST['slugTerm'];
        $paged = $_POST['page'];
        $loop = new WP_Query( array( 'post_type' => '', 'tag' => $term_slug,
            'posts_per_page' => 1, 'paged' => $paged, ) ); ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
            <li class="blog-box">
                <div class="blog-bullet-img">
                    <a href="<?php the_permalink(); ?>" title="<?php
                    the_title_attribute(); ?>">
                        <?php the_post_thumbnail(); ?>
                    </a>
                </div>
                <div class="news-full-descrip">
                    <p class="blog_title"><?php the_title(); ?></p>
                    <p class="blog_date"><?php echo get_the_date(); ?></p>
                    <p class="blog-textarea"><?php echo get_the_excerpt(); ?></p>
                </div>
            </li>
    
    
        <?php endwhile;  wp_die(); }