Support » Themes and Templates » Custom query before default query in single-posttype.php

  • Resolved OrT

    (@ort)


    Beloved WordPressers,

    In single-posttype.php, I’m doing a custom query before the regular <?php if (have_posts()) : while (have_posts()) : the_post(); ?>.

    [please mark any code in your post – see http://codex.wordpress.org/Forum_Welcome#Posting_Code ]

    Before the custom query, I clone the wp_query:

    $temp =  clone $wp_query;

    After the custom query, I set it back and erase postdata:

    <?php $my_query = null; $my_query = clone $temp; ?>
    <?php wp_reset_postdata(); ?>

    But the detail post that should be showing shows all the posts that the first query (custom query) finds.

    What am I doing wrong?
    Thanks a lot for your time, much obliged!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Michael

    (@alchymyth)

    Forum Moderator

    are you using $my_query for the secondary loop?

    possibly just a typing mistake; and maybe rearrange the code:

    <?php wp_reset_postdata(); ?>
    <?php $my_query = null; $wp_query = clone $temp; ?>
    Chip Bennett

    (@chipbennett)

    Theme Review Admin

    How are you running your custom query, via new WP_Query() (correct), or via query_posts() (incorrect)?

    If you’re using query_posts():

    1. Don’t do that. That function is intended only to modify the primary loop query.
    2. Use WP_Query() instead.
    3. Then, wp_reset_postdata() will work as expected

    Ok, that was very stupid of me. After hours of searching and replacing.
    Thank you for that, alchymyth 🙂

    Chip – not that it matters now, but I was running WP_Query().
    Thanks for your time, though!

    Chip Bennett

    (@chipbennett)

    Theme Review Admin

    not that it matters now, but I was running WP_Query()

    Music to my ears! 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom query before default query in single-posttype.php’ is closed to new replies.