0

I have a code that return live post updates to me, but there is something wrong on it.

When new post published it's get under other post, i want to have it over other posts.

My code:

<div class="latest-news">
      <?php 
        $current_posts_ids = array();
        $args = array(
          'post_type' => 'post',
          'post_status' => 'publish',
        );
        $my_posts = new WP_Query( $args );

        if ( $my_posts->have_posts() ) : ?>
          <div class="posts">
            <?php while ( $my_posts->have_posts() ) : $my_posts->the_post() ?>
              <?php array_push($current_posts_ids, get_the_ID() );?>
              <article data-id="<?php echo get_the_ID()?>">
                <h2><?php the_title() ?></h2>
                <p><?php the_excerpt() ?><p>
              </article>
            <?php endwhile ?>
          </div>
        <?php endif ?>
 </div>

 <script>
  $(document).ready(function(){

      var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";   
      var dataArray =  <?php echo json_encode($current_posts_ids) ?>;

      function getMyPosts() {

            data = { action: "update_news", data: dataArray};
            $.post(ajaxurl, data, function(response) {
                // reset array
                dataArray = [];
                // append the new news posts                
                $('.latest-news .posts').append(response);
                // create new array of ids to exclude
                $('article').each(function(){
                    dataArray.push($(this).data('id'));  
                }); 
                // repeat function after 5 seconds
                setTimeout(function () {
                    getMyPosts();
                }, 5000)
            });

     }
     //run the function first time
     getMyPosts();   

});
</script>
1
  • Took a quick look and I think you can just change append to prepend in your jQuery on this line $('.latest-news .posts').append(response);
    – Wilco
    Commented Nov 14, 2017 at 9:10

1 Answer 1

0

So you can use the prepend method:

 <script>
  $(document).ready(function(){

      var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";   
      var dataArray =  <?php echo json_encode($current_posts_ids) ?>;

      function getMyPosts() {

            data = { action: "update_news", data: dataArray};
            $.post(ajaxurl, data, function(response) {
                // reset array
                dataArray = [];
                // append the new news posts                
                $('.latest-news .posts').prepend(response);
                // create new array of ids to exclude
                $('article').each(function(){
                    dataArray.push($(this).data('id'));  
                }); 
                // repeat function after 5 seconds
                setTimeout(function () {
                    getMyPosts();
                }, 5000)
            });

     }
     //run the function first time
     getMyPosts();   

});
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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