WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_posts() ignoring parameters (3 posts)

  1. f00bar
    Member
    Posted 4 years ago #

    Hello everyone, wondered if someone could shed some light on this.

    I have referenced the page for the function get_posts() in order to be able to grab the list of posts from a particular category (in this case 'news') but for some reason it's returning a random post in a completely different category.

    My code is as follows:

    <?php
     global $blog;
     $myarticles = get_posts('numberposts=3&offset=0&category_name=news');
     foreach($myarticles as $article) :
     ?>
       <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
    	<?php the_date(); ?><br />
    
     <?php endforeach; ?>

    Here I am grabbing all the News Articles I have written and placing the most recent three on the page, but it's not doing that - instead it's putting the title of one random post from my database. Can't understand why - is there something I'm missing?

    Thanks in advance for any help.
    Michael

  2. MichaelH
    Member
    Posted 4 years ago #

    Change:

    foreach($myarticles as $article) :

    To:

    foreach($myarticles as $post) :
    setup_postdata($post);
  3. f00bar
    Member
    Posted 4 years ago #

    Great thanks, that worked!

Topic Closed

This topic has been closed to new replies.

About this Topic