WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom query before default query in single-posttype.php (6 posts)

  1. OrT
    Member
    Posted 2 years ago #

    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!

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    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; ?>
  3. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    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

  4. OrT
    Member
    Posted 2 years ago #

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

  5. OrT
    Member
    Posted 2 years ago #

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

  6. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

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

    Music to my ears! :)

Topic Closed

This topic has been closed to new replies.

About this Topic