WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] the_tags() in a Page Template (3 posts)

  1. jimwhimpey
    Member
    Posted 6 years ago #

    I made a page template. Inside the page template is a custom query to grab a random post. I make a page, I used that page template and everything is great apart from the_tags() template tag breaking. I imagine this is because it's a page, which doesn't use tags and not a "post" page.

    This is the custom loop code:

    <?php
    $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY RAND() LIMIT 1");
    $my_query = new WP_Query("p=$rand_id");
    ?>

    The single random post grabbing is working fine, it's just the the_tags() I need to work. Maybe there's another way to query that'll let me use the_tags().

    Thanks

  2. Kafkaesqui

    Posted 6 years ago #

    Seems that a custom query created through the WP_Query class is not seen as a post loop (which the_tags() tests for), even though the same would work with query_posts(). Possibly a bug.

    Anyway, try adding this right after your $my_query line:

    $wp_query->in_the_loop = true;

  3. jimwhimpey
    Member
    Posted 6 years ago #

    Kafkaesqui, thanks! That line worked like a charm!

Topic Closed

This topic has been closed to new replies.

About this Topic