WordPress.org

Ready to get started?Download WordPress

Forums

wp_query collision in multiple loop site (3 posts)

  1. w.davis
    Member
    Posted 1 year ago #

    I've done all I can so now it's time to come hat in hand for help.

    I have a site that is not a blog but uses some blogging elements. I have 2 loops on the front page, one is a sidebar for announcements (like a baby was born at x) the other is for longer blog type posts. I only have 2 categories defined (cat 3 main_posts and cat 4 announcement).

    My problem is that the announcements (2nd loop) is filling with posts from the first. There is currently only one announcement in the DB but I still should only see that one post from the second loop.

    I've tried all I know, including wp_reset_query and wp_reset_postdata.

    You can veiw the site here davisob.com . The site is still in a rough state so be gentile. Consequently its my first website ever.

    Code below:

    First Loop (main_posts)

    <?php if(have_posts()): ?>
    <?php   // ***  Get's page variables or initializes if initial visit
    $my_post_id = isset($_GET["p"])?$_GET["p"]:"0";
    $mypage = isset($_GET["mypage"]) ? $_GET["mypage"] : "1";
    $ann_page=isset($_GET["ann_page"])?$_GET["ann_page"]: "1";
    // *** Sets up Page Index
    $myposts = new WP_Query("&cat=3");
    $posts_count = $myposts->post_count;
    $total_pages = (int) ($posts_count / 4);
    $extra_page =  (int) ($posts_count % 4);  // could have just used a ceiling function
    if ($extra_page > 0) {
    $total_pages++;
    }
    if ($mypage == 1) {
    $prev_nav_string = "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
    }
    else {
    $prev_nav_string = "<a href=\"./index.php?mypage=" . ($mypage - 1) . "&ann_page=" . ($ann_page) . "&p=0 \">&laquo Prev Posts</a>";
    }
    if ($mypage == $total_pages) {
    $next_nav_string = "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
    }
    else {
    $next_nav_string = " <a href=\"./index.php?mypage=" . ($mypage + 1) . "&ann_page=" . ($ann_page) . "&p=0 \">Next Posts &raquo</a>";
    }
    //  *** Sets the query based on a single post or front page load ****
    if ($my_post_id>0){
    $myposts = new WP_Query();							$myposts->query("p=" . $my_post_id );
    $next_nav_string = "-";
    $prev_nav_string = "-";
    }
    else{
    $myposts = new WP_Query();
    $myposts->query("showposts=4" . "&order=DESC" . "&orderby=date" . "&cat=3" . "&paged=" . $mypage);
    }
    //*********************************************************************
    //*****************************   Start Word Press Loop ***************
    //*********************************************************************
    $wp_query = $myposts;
    while(have_posts()):the_post();
    ?>
    <!-- create post div -->
    <div <?php post_class(); ?> id="post-<?php the_ID() ;?>">
    <!-- Create Post title and credits -->
    <h2 class="post">
    <a title="<?php the_title(); ?>" href="<?php the_permalink(); echo "&mypage=$mypage&ann_page=$ann_page \" >" ?><?php the_title(); ?></a>
    </h2>
    <h3 class="post"> <?php the_time('F jS, Y'); echo"&nbsp&nbsp By:"; the_author(); ?></h3>
    <!-- Create Content Div and Fill it -->
    <div id="content-<?php the_ID();?>" class="blog_post_body">
    <?php the_content('{Read More}'); ?>
    </div>
    <?php //wp_link_pages() sets the pagination for posts that are more than 1 page ?>
    <?php wp_link_pages('<p class="pages"><strong>'.__('Pages:').'</strong> ', '</p>', 'number'); ?>
    <?php // Create facebook comments link ?>
    <div class="postmeta">
    <div class="postcomment"><?php comments_popup_link('No Comments', '<span class="comnum">1</span> Comment', '<span class="comnum">%</span> Comments'); ?></div>
    </div>
    <div class="postmeta_bottom"> </div>
    </div>
    <?php // Create facebook comment block if on a single post ?>
    <div class="comments_template">
    <?php if (function_exists('facebook_comments'))facebook_comments(); ?>
    </div>
    <?php echo"<img class='divider_3large' src='images/divider_small.png' alt='divider_small.png' />"; ?>
    <?php endwhile; ?>
    <?php // End of WordPress while loop
    // Reset Searches
    wp_reset_postdata();
    wp_reset_query();
    $myposts=NULL;
    ?>
    <!-- Sets up page Navigation Links -->
    <div id="blog_nav_wrapper" class="navclass">
    <div id="blog_nav" class="navclass">
    <span id="prev_nav" class="navclass"> <?php echo $prev_nav_string ; ?>  </span>
    <span id="page_nav" class="navclass">
    <?php
    if($my_post_id>0){
    echo "<a class=\"navclass\" href=\" ./index.php?mypage=" . $mypage. "&ann_page=" . $ann_page . "&p=0 \" > RETURN </a> " ;
    }
    else{
    for ( $nav_page_index=1; $nav_page_index <= $total_pages; $nav_page_index++){
    if($mypage==$nav_page_index){
    echo "<span class=\"navclass\"> $nav_page_index </span>";
    }
    else{
    echo "<a class=\"navclass\" href=\" ./index.php?mypage=" . $nav_page_index . "&ann_page=" . $ann_page . "&p=0 \" > " . $nav_page_index  . "</a>" ;
    }
    }
    }?>
    </span>
    <span id="next_nav" class="navclass"><?php echo $next_nav_string ; ?> </span>
    </div>
    </div>
    <div style="clear:both;"></div>
    <?php else: ?>
    <!-- 404 Error if next page is not found -->
    <h2><div class="error"><?php _e('Not Found'); ?></div></h2>
    <?php endif ?>
    <?php // *********************  End WordPress Loop Hack ****************************** ?>

    Second Loop (Announcements)

    <?php //**********************************Begin WordPress Loop Hack************************************ ?>
    <?php if(have_posts()): ?>
    <?php
    // ***  Get's page variables or initializes if initial visit
    $my_post_id = isset($_GET["p"])?$_GET["p"]:"0";
    $mypage = isset($_GET["mypage"]) ? $_GET["mypage"] : "1";
    $ann_page=isset($_GET["ann_page"])?$_GET["ann_page"]: "1";
    // *** Sets up Page Index
    $annposts = new WP_Query("&cat=4");
    $posts_count = $annposts->post_count;
    $extra_page =  (int) ($posts_count % 10);  // could have just used a ceiling function
    if ($extra_page > 0) {
    $total_pages++;
    }
    if ($ann_page == 1) {
    $prev_nav_string ="&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
    }
    else {
    $prev_nav_string = "<a href=\"./index.php?mypage=" . ($mypage) . "&ann_page=" . ($ann_page - 1) . "&p=0 \">&laquo Prev Posts</a>";
    }
    if ($ann_page == $total_pages) {
    $next_nav_string ="&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
    }
    else {
    $next_nav_string = " <a href=\"./index.php?mypage=" . ($mypage) . "&ann_page=" . ($ann_page + 1) . "&p=0 \">Next Posts &raquo</a>";
    }
    //  *** Sets the query based on a single post or front page load ***
    if ($my_post_id>0){
    $annposts = new WP_Query();
    $annposts->query("p=" . $my_post_id );
    $next_nav_string = "-";
    $prev_nav_string = "-";
    }
    else{
    $annposts = new WP_Query();
    $annposts->query("showposts=10" . "&order=DESC" . "&orderby=date" . "&cat=4" . "&paged=" . $ann_page);
    }
    //*********************************************************************//*****************************   Start Word Press Loop  **************
    //*********************************************************************
    $wp_query = $annposts;
    while(have_posts()):the_post();
    ?>
    <!-- create post div -->
    <div <?php post_class(); ?> id="post-<?php the_ID() ;?>">
    <!-- Create Post title and credits -->
    <?php the_date(); echo "<br />"; ?>
    <?php  echo"<h2 class=\"announce\"> "; the_title(); echo"</h1>"; ?>
    <?php echo"<text class=\"announce\"style='font-size:30%'>"; get_the_date(); echo"&nbsp&nbsp"; get_the_time();echo"</text>"; ?>
    <div id="content-<?php the_ID();?>" class="announce">
    <?php the_content(); ?>
    </div>
    </div>
    <!-- This sets up the comments div -->
    <div class="comments_template">
    <?php //comments_template(); ?>
    <?php if (function_exists('facebook_comments'))facebook_comments(); ?>
    </div>
    <?php echo"<img class='divider_3large' src='images/divider_small.png' alt='divider_small.png' />"; ?>
    <?php endwhile; ?>
    <?php // End of WordPress while loop
    // Reset Searches
    wp_reset_postdata();
    wp_reset_query();
    $annposts=NULL;
    ?>
    <!-- Sets up page Navigation Links -->
    <div id="blog_nav_wrapper" class="announce">
    <div id="blog_nav" class="announce">
    <span id="prev_nav" class="announce"> <?php echo $prev_annav_string ; ?>  </span>
    <span id="next_nav" class="announce"><?php echo $next_annav_string ; ?> </span>
    </div>
    </div>
    <div style="clear:both;"></div>
    <?php else: ?>
    <!-- 404 Error if next page is not found -->
    <h2><div class="error"><?php _e('Not Found'); ?></div></h2>
    <?php endif ?>
    <?php // *********************  End WordPress Loop Hack ****************************** ?>
  2. w.davis
    Member
    Posted 1 year ago #

    I've found the issue but not a solution. Im using the plugin "Facebook Comments for WordPress" by we8u. It looks like the plugin is holding on to the first query and never clears it after a loop is finished. When the next loop is called it works on the new query and the old.

  3. w.davis
    Member
    Posted 1 year ago #

    Digging further into this, he is calling global $wp_query in his code rather than taking the query passed into the loop. Outside the loop I've defined custom query's and set $wp_query to the custom objects. (Probably not the best way, but when I was passing my custom objects via $object->have.has_posts caused an issue where every comment link (fbcomments again) was wired to the top most post in the query.... ie they all had &id=45.

    Still hopping for a stroke of luck.

Topic Closed

This topic has been closed to new replies.

About this Topic