0

I have created a new CPT called Event and inside event the taxonomy ( or term I dont know the correct name) called month with 12 months inside.

So far I managed to display all month and filter it by ajax, but inside it month it is display only 3 posts per time and when I click in load more ajax button it ignores the taxonomy-month.php and displays all the events. It works fine in the view all, but not inside each month.

Please take a look in the january tab as example: http://esma1.staging.wpengine.com/trade-fairs/

my taxonomy-month.php

<div id="post-ajax" class="eventb">  
                   <div id="inside">  
                          <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                              <section class="blocks">
                                   <div class="content_element">
                                       <a href="<?php echo get_permalink();?>"><h2><?php the_title(); ?></h2></a>
                                       <div class="content_text">
                                            <p><?php the_content(); ?></p>
                                            <p>Month: <?php the_terms( $post->ID, 'month', ' ', ' / ' ); ?></p>
                                        </div>
                                    </div>
                                    <div class="single_image_element">
                                         <div class="imgwrap"><div class="memberimg"><?php the_post_thumbnail(); ?></div></div>
                                   </div>
                              </section>
                            <?php endwhile; endif; ?>           
                   </div>
                   <div class="clear"></div>
       <div class="post_nav">
          <div class="loadmore">Load More</div>
       </div>

Script

var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
            var page = 2;
           jQuery(function($) {
               $('body').on('click', '.loadmore', function() {
               var data = {
    'action': 'load_posts_by_ajax',
    'page': page,
    'security': '<?php echo wp_create_nonce("load_more_posts"); ?>'
};

$.post(ajaxurl, data, function(response) {
    $('#inside').append(response);
    page++;
});
               });
            });

FUNCTION

function load_posts_by_ajax_callback() {
   check_ajax_referer('load_more_posts', 'security');
   $paged = $_POST['page'];
   $args = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => '3',
'paged' => $paged,
     );
$my_posts = new WP_Query( $args );
if ( $my_posts->have_posts() ) :
    ?>
<article>
<?php while ( $my_posts->have_posts() ) : $my_posts->the_post() ?>
          <section class="blocks">

                       <div class="content_element">

                           <a href="<?php echo get_permalink();?>"><h2><?php the_title(); ?></h2></a>
                             <div class="content_text">
                       <p><?php the_content(); ?></p>
                               <p>Month: <?php the_terms( $post->ID, 'month', ' ', ' / ' ); ?></p>
                                </div>
                        </div>

                            <div class="single_image_element">
                            <div class="imgwrap"><div class="memberimg"><?php the_post_thumbnail(); ?></div></div>
                           </div>

                    </section>
<?php endwhile ?>
</article>
1

Assuming the function hooked to the action load_posts_by_ajax is your load_posts_by_ajax_callback() code (seems a reasonable assumption), the query arguments can be easily modified.

You mention "displays only 3 posts" which is what the code is designed to do. If you wish to show 10 posts, update the posts_per_page argument listed.

'posts_per_page' => '3', 

The bigger portion of your question is addressed by a taxonomy query. This is how you limit your query results to a specific term (january) within a specific taxonomy (month). I am guessing on the term slug and taxonomy name in this answer, however.

Assuming there is a month taxonomy registered for the event post type, something like this for your query should work:

function load_posts_by_ajax_callback() {
  check_ajax_referer('load_more_posts', 'security');
  $paged = $_POST['page'];
  $args = array(
    'post_type' => 'event',
    'post_status' => 'publish',
    'posts_per_page' => '10', // assuming you want 10 posts returned
    'tax_query' => array( // a tax query is always an array of arrays
      array(
        'taxonomy' => 'month', // assuming again; your taxonomy slug
        'field' => 'slug',
        'terms' => 'january', // you will want to assign this programmatically from the JS; not evident in the code provided
      )
    ),
    'paged' => $paged,
  );

 $my_posts = new WP_Query( $args );
 // remainder of function code is unchanged...

Several assumptions made in this answer. If you can add the relevant code to your question I might be able to update.

EDIT: I'm not 100% confident in the paging aspect. The code you provided makes use of paging and IIRC, this should be done with the offset argument for WP_Query. Separate question perhaps? Let's solve the Ajax tax query first.

|improve this answer|||||
  • Thank you for replying... I want to display 3 post per time, it was just myself explain it. I dont know if you saw the site, but this ajax function is working inside all the months. Is there a way to get the terms dynamic? or shouldI create a function for each month? – Leandro Andrade May 30 '18 at 21:08
  • Sorry @LeandroAndrade but I wasn't able to get back to this quickly. Did you sort this out already or do you still need help? – jdm2112 Jun 7 '18 at 13:45

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.