• I’ve been hunting and trying for a couple days now.

    In one unresolved forum thread, Otto42 was close to solving my problem, but the user fell off. Otto42 had asked the user to explain intentions. here’s my attempt

    I have 3 categories: feature(cat=14), style(cat=10) , and music(cat=11)
    A post has to be in either music or style or both, or all three,
    but a post cannot be in just feature.

    (currently, i have 9 posts, 4 music, 4 style, and 1 in feature and style)

    on my home.php

    I want to show 1 post from the category= feature(cat=14)

    Then show 3 most recent posts from the category style(cat=10)

    then 3 from the category music(cat=11)
    without duplicating a single post.

    (i’ve got the CSS stuff under control)

    The code below is the closest I got.
    I wasn’t able to maintain the $do_not_duplicate variable across all three loops.
    I tried the super loop, the triple loop, etc..

    (i’m running the site, on MAMP so i can’t send link:

    <?php get_header(); ?>
    
    <!-- MODULE 02 -->
    <div id="maincontent">
    	<div id="rightwindow">
    	<?php get_sidebar(); ?>
    	</div><!-- div id right window-->
    	<div id="leftwindow">
    		<div id="thefeature">
    	     	<h2> This is the start page </h2>
    	 	<?php $temp_query = clone $wp_query; ?>
    	 	<!-- Do stuff... -->
    
    	 	<?php $my_query = new WP_Query('cat=14&showposts=1');
    	 	while ($my_query->have_posts()) : $my_query->the_post(); ?>
    	   <!-- Do special_cat stuff... -->
    			<?php the_title(); ?>
    			<?php the_excerpt(); ?>
    	 	<?php endwhile; ?>
    
    	 	// now back to our regularly scheduled programming
    	 	<?php $wp_query = clone $temp_query; ?>
    
    	 		</div> <!-- div id thefeature -->
    		<br class="clearfloat" />
    
    		<div id="sectionwrap">
    			<div id="section2">
    		 		<h2> STYLE STYLE SECTION 02</h2>
    			<?php $temp_query2 = clone $wp_query; ?>
    		 		<?php $my_query2 = new WP_Query('showposts=3&cat=10&cat=-14');
    				 	while ($my_query2->have_posts()) : $my_query2->the_post(); update_post_caches($posts); ?>
    					 <!-- Do special_cat stuff... -->
    							<?php the_title(); ?>
    							<?php the_excerpt(); ?>
    					<?php endwhile; ?>
    					// now back to our regularly scheduled programming
    					<?php $wp_query = clone $temp_query2; ?>
    			</div> <!-- div id section2 -->
    			<div id="section1">
    				<!-- Do other stuff... -->
    				<h2> MUSIC MUSIC SECTION 01 </h2>
    				<?php $temp_query3 = clone $wp_query; ?>
    			 		<?php $my_query3 = new WP_Query('showposts=3&cat=-10&cat=11&cat=-14');
    					 	while ($my_query3->have_posts()) : $my_query3->the_post(); update_post_caches($posts); ?>
    						 <!-- Do special_cat stuff... -->
    								<?php the_title(); ?>
    								<?php the_excerpt(); ?>
    						<?php endwhile; ?>
    						// now back to our regularly scheduled programming
    						<?php $wp_query = clone $temp_query3; ?>
    
    			</div> <!-- div id section1 -->
    		</div> <!-- div id sectionwrap -->
    	</div> <!-- div id leftwindow-->
    
    	</div> <!-- div id maincontent-->
    	<br class="clearfloat" />
    
    	<div id="footer">
    		<h4> THis is the footer crap </h4>
    		<!-- >$your_query = new WP_Query('showposts=1&cat=3&paged='. $paged); -->
    		<?php get_footer() ?>

    thanks in advance

Viewing 5 replies - 1 through 5 (of 5 total)
  • Just curious.. why you exclude categories using the – sign. Instead you can just include posts from specific category by ID or by name without worrying about duplicate posts.

    This is what i would do..this is just me.. 🙂

    $catone = get_posts('numberposts=3&category=11');
    foreach ($catone as $post) {
    ... print out post conten ...
    }
    
    $cattwo = get_posts('numberposts=3&category=13');
    foreach ($cattwo as $post) {
    ... print out post conten ...
    }

    and so on. I never had conflicts with this way.

    Thread Starter icarus313

    (@icarus313)

    thanks, I appreciate any help.
    And you are correct, the cat=-14, code was a inelegant and ineffective method for preventing duplicates.
    I’ve since stepped back, and removed the feature category, and created a tag=fresh.
    As well as got the $do_not_duplicate variable to work.

    Now my problem is that to get the desired effect, I have to add an offset=1 parameter, to ONLY the category that happens to have the post with the tag=fresh, otherwise I lose a post in one or both of the category loops.

    I know I’m over thinking and/or overcoding, but i’m coming from actionscript and xml , and new to php.

    I really just want to:
    1. grab 10 of the most recent posts (admin setting)
    2. from the 10 posts extract the latest post tagged = fresh
    3. style that extracted post <div id=”fresh”>
    4. with the remaining 9 posts extract the 3 most recent posts in category = style
    5. style those 3 posts <div id=”mystyle”>
    6. with the remaining 6 extract the 3 most recent posts in category = music
    7. style those 3 posts <div id=”mymusic”>
    8. with the reamining posts, eiter ignore or list on sidebar

    I could do this with Actionscript and XML, i’mjust having trouble grasping how to do this in WP (without a plugin)

    I’ve since implemented:

    <?php $temp_query = clone $wp_query; ?>
    	 <?php $my_query = new WP_Query('tag=fresh&showposts=1');
    	 	while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate[] = get_the_ID(); ?>
    <!-- displaying the latest post tagged "fresh" -->
    			<?php the_title(); ?>
    			<?php the_excerpt(); ?>
    	 <?php endwhile; ?>
    <!-- here is loop for cat=11 minus the post stored in variable $do_not_duplicate -->
    
    <?php $temp_query2 = clone $wp_query; ?>
    <?php $my_query2 = new WP_Query('showposts=3&cat=11&offset=1');
    while ($my_query2->have_posts()) : $my_query2->the_post();
    if (array_search(get_the_ID(), $do_not_duplicate) !== false)
    continue; update_post_caches($posts); ?>
    <!-- STyle Style -->
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    <?php endwhile; ?>
    // now back to our regularly scheduled programming
    <?php $wp_query = clone $temp_query2; ?>
    
    	<?php $temp_query3 = clone $wp_query; ?>
    <?php $my_query3 = new WP_Query('showposts=3&cat=10);
    while ($my_query3->have_posts()) : $my_query3->the_post();
    if (array_search(get_the_ID(), $do_not_duplicate) !== false)
    continue; update_post_caches($posts); ?>
     <!-- category = music  -->
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    <?php endwhile; ?>
    // now back to our regularly scheduled programming
    <?php $wp_query = clone $temp_query3; ?>

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    I liked your first approach better, but I’d modify it slightly:

    new WP_Query(array(
    'cat__in'=>array(14),
    'showposts'=>1,
    ));
    ...
    new WP_Query(array(
    'cat__in'=>array(10),
    'cat__not_in'=>array(14),
    'showposts'=>3,
    ));
    ...
    new WP_Query(array(
    'cat__in'=>array(11),
    'cat__not_in'=>array(10,14),
    'showposts'=>3,
    ));

    Or use tag__in and tag__not_in if that makes more sense. These will work better with cat__* in 2.7 though. Might consider upgrading now instead of later.

    Also, the “clone” bits are unnecessary. $wp_query doesn’t get touched unless you touch it or use query_posts(). And even then, WP keeps it around, so a wp_reset_query() call will fix it faster.

    Thread Starter icarus313

    (@icarus313)

    Thanks, Otto42 !

    I’ve been reading your responses…
    and i’ve learned a lot.

    These will work better with cat__* in 2.7 though. Might consider upgrading now instead of later.

    I definitly want to upgrade asap, but not sure how to get to upgrade to 2.7? don’t see it in beta.

    thnaks

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    It’s still being done as nightly builds, not up to beta status yet.

    I’m running the trunk off SVN: http://codex.wordpress.org/Installing/Updating_WordPress_with_Subversion

    Still has a few bugs, but it’s rapidly improving.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘WP_Query, three iterations of theLoop’ is closed to new replies.