Forums

using get_posts twice on a page? (5 posts)

  1. mensky
    Member
    Posted 3 years ago #

    I'm trying to setup two separate sections on a page using get_posts and pulling different groups of posts based on a custom field. Each get_posts works independently, but when both on the same page, only the first works properly. I"m thinking this is a variable problem, but still very new to PHP and not sure how to easily remedy.

    <!-- Latest News -->
    <?php
     $news_tag = get_post_meta($post->ID, 'news-tag', true);
     $postslist = get_posts('tag='.$news_tag.'&numberposts=10&order=ASC&orderby=date');
     foreach ($postslist as $post) :
        setup_postdata($post);
     ?>
     <div>
     <?php the_date(); ?> - <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a>
     </div>
     <?php endforeach; ?>
    <!-- End News -->
    
    <!-- Upcoming Events -->
    <?php
     $events_tag = get_post_meta($post->ID, 'events-tag', true);
     $postslist = get_posts('tag='.$events_tag.'&numberposts=10&order=ASC&orderby=name');
     foreach ($postslist as $post) :
        setup_postdata($post);
     ?>
     <div>
     <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a>
     </div>
     <?php endforeach; ?>
     <!-- End Events-->
  2. mensky
    Member
    Posted 3 years ago #

    Can these maybe be combined?

  3. keepthingsclean
    Member
    Posted 3 years ago #

    Hi there,

    I encountered a similar problem. I wrote a custom navigation and queried also "get_posts()" and used them with a variable name $posts.
    As long as I did not change it, all the following standard queries from TheLoop were broken.
    So try altering your variable names as well.

  4. anjani1234
    Member
    Posted 3 years ago #

    I encounterd a similar problem but it is resolved for me. we have to seperate two section with two functions i.e for one section we have to use query_post() and for another section we have to use get_post(). i got output like this.

  5. MadsK
    Member
    Posted 3 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic