0

i need to get the wordpress posts while click the load more button or something else, so i have followed the tutorial by Here

<?php
/*
        Template Name: Additional Posts
    */
$offset = htmlspecialchars(trim($_GET['offset']));
    if ($offset == '') {
        $offset = 5;
    }

    $args = 'post_type=event&posts_per_page=3&offset=$offset'; 

                     $my_query = new WP_Query($args);
                    if ( $my_query->have_posts() ) {
                    while ($my_query->have_posts()) : $my_query->the_post(); 

                $event_id = get_post_meta( get_the_ID(), '_event_id', true );
                $location_id = get_post_meta( get_the_ID(), '_location_id', true );
                $custom_fields = get_post_custom(get_the_ID());

                global $wpdb;
                $em_states = $wpdb->get_results("SELECT *from  ".EM_LOCATIONS_TABLE." WHERE location_id = '$location_id' ");
                $em_ticket_price = $wpdb->get_results("SELECT *from  ".EM_TICKETS_TABLE." WHERE event_id = '$event_id' ");
                ?>                
                    <div class="filter-box clearfix post">
                      <div class="row-fluid">
                        <div class="span4"><?php echo get_the_post_thumbnail(get_the_ID(), array(269,168) ); ?> </div>
                        <div class="span5">
                          <h5><span class="switch_rest"><?php echo $em_states[0]->location_town;?> -</span> <?php the_title(); ?></h5>
                          <ul class="clearfix">
                            <li><i class="icon-time pad-rht"></i><?php  $star_time = get_post_meta( get_the_ID(), '_start_ts', true );  echo date('g:i:s a ',$star_time);?>-<?php   $end_time = get_post_meta( get_the_ID(), '_end_ts', true );  echo date('g:i:s a ',$end_time); ?> </li>
                            <li> <i class="icon-folder-open pad-rht"></i><?php 
                            $terms = get_the_terms(get_the_ID(), 'event-categories' );
if ($terms && ! is_wp_error($terms)) :
    $term_slugs_arr = array();
    foreach ($terms as $term) {
        $term_slugs_arr[] = $term->name;
    }
    $terms_slug_str = join( " | ", $term_slugs_arr);
endif;
echo $terms_slug_str;?> </li>
                            <li><i class="icon-user pad-rht"></i><?php echo $custom_fields['wpcf-from'][0]."-".$custom_fields['wpcf-to'][0]; ?>  </li>
                          </ul>
                        </div>
                        <div class="span3">
                          <div class="price-box clearfix">
                            <h4>Price Range</h4>
                            <ul class="inline clearfix">
                            <!--  <li>From</li>-->
                              <li><?php echo $em_ticket_price[0]->ticket_price; ?>&euro;</li>
                              <br />
                           <!--   <li>To </li>
                              <li>100&euro;</li>-->
                            </ul>
                          </div>
                        </div>
                      </div>
                    </div>
                    </div>
   <?php endwhile; } ?>                       

and my jquery is follows

 <script type="text/javascript">
        var $j = jQuery.noConflict(); 

    $j(function(){

        var offset = 0;


       // $j("#postContainer").load("<?php echo get_site_url(); ?>/additional-posts/?offset="+offset);
        $j("#another").click(function(){

            offset = offset+3;

            $j("#postContainer")
               // .slideUp()
                .load("<?php echo get_site_url(); ?>/additional-posts/?offset="+offset, function() {
                    $j(this).slideDown();
                });

            return false;
        });

    });
    </script>

it is working, but while click the get more link, am again getting the same posts as well as in the my original loop here in my page , i know that paged query is missing, or else what am missing ?

  • 2
    Variables don't expand inside single quotes, you are literally passing the string $offset as offset in $args. also, Here is a better way to load posts via AJAX, using the WordPress AJAX API. – Milo Sep 27 '13 at 15:26
  • OK, there what script.js? where can i get it? – jothikannan Sep 27 '13 at 15:44
  • @Milo i got it and it is working, but am getting a error, all my posts in drafts also listing from the loaded posts? what is wrong here? – jothikannan Sep 27 '13 at 15:51
  • also can i add a loader effet here? and no more posts? Thanks for great reply – jothikannan Sep 27 '13 at 15:53
  • 2
    you shouldn't be seeing drafts, it sounds like something else is modifying the query. you can try to explicitly load posts with post_status = publish. for loader effect, add a class or whatever before requesting more posts, then remove it within the callback that adds the data to the page. you can check the member variable found_posts to know if you are on last page - $wp_query->found_posts. add offset + posts per page, if that is equal or greater than found posts, there are no more posts to load. – Milo Sep 27 '13 at 16:08

Your Answer

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

Browse other questions tagged or ask your own question.