WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] setup_postdata() and the_tags() (5 posts)

  1. andersson
    Member
    Posted 6 years ago #

    It doesn't seem like the_tags() function is working correctly if you use setup_postdata() to retrieve the data on each posts.

    The following works fine:
    foreach ($posts as $post) : the_post(); and then you can call the_tags() and everything's just fine.

    The following does not seem to work:
    foreach($posts as $post) : setup_postdata($post);
    the_tags() produces nothing when used in such foreach-loop.

    Why would you want to use the latter instead of the first example? Well, say you want to execute this:

    $posts = get_posts('numberposts=3&category=12&orderby=post_date&order=DESC');

    This is probably used by ppl who are using wordpress as a cms rather than just a blog. So the vanilla seems to work but if you step of the track just a little bit it seems to not work.

    Could this please be confirmed or denied? Thanks much.

  2. andersson
    Member
    Posted 6 years ago #

    Solved.

    If you do this:

    global $wp_query;
    $wp_query->in_the_loop = true;

    before you start calling get_posts(%custom_code_here%) then you can use the_tags().
    For reasons I do not know there's a check

    if ( ! $id && ! in_the_loop() )
        return false; // in-the-loop function

    in the get_the_tags() method. Seems redundant since a little further down the code it checks if $tags is empty and if so returns false. That would catch any erroneous (i.e. empty $posts) calls to the_tags one would think.

    Well, it's solved now but if anyone can benefit from this monologue, all the better. :)

  3. shacker
    Member
    Posted 6 years ago #

    Anderson - Wow, that's a weird one. Your fix works fine on 2.5. Never would have figured that out on my own, thanks.

  4. thecoup
    Member
    Posted 6 years ago #

    Andersson, this is great. Just to confirm, you're using this outside the primary loop, before calling a unique get_posts() query?

    Do you think this works the same for query_posts()? I haven't been able to make get_posts() work with the more quicktag.

  5. Ivovic
    Member
    Posted 6 years ago #

    this should be posted as a bug report.

Topic Closed

This topic has been closed to new replies.

About this Topic