Support » Developing with WordPress » setup_postdata() and the_tags()

  • Resolved andersson


    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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. 🙂

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

    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.

    this should be posted as a bug report.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘setup_postdata() and the_tags()’ is closed to new replies.