WordPress.org

Ready to get started?Download WordPress

Forums

Exclude latest post in a category from showing up in the loop? (12 posts)

  1. geezerd
    Member
    Posted 4 years ago #

    Is that possible?

    I've made it so the latest post from "misc" and the latest post from "selfdefense" are in 2 boxes in my header, with this:

    <?php if (have_posts()) : ?>
     <?php $my_query = new WP_Query('category_name=misc&posts_per_page=1');
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    <?php endwhile; endif; ?>

    then repeat that code, using category_name=selfdefense instead.

    OK, so then, below that, I don't want the loop with all the rest of the posts to show those 2 latest from those categories, but I DO want to see the 2nd to latest, and so on, from those categories, as normal.

    What can put in the loop to make it exclude just those 2 latest posts from those categories?

    Thanks in advance!

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    Store each post id that you're looping over in the custom loops into an array, when you do your normal loop load that array into the query_posts "post__not_in" parameter to exclude them from the query...

    Trimmed example:

    <?php
    	// Create empty array to store post ids in
    	$excludes = array();
    
    	// Query one
    	$my_query = new WP_Query('category_name=NAME1&posts_per_page=1');
    	// If this query has posts
    	if($my_query->have_posts()) :
    		// Loop over the posts
    		while ($my_query->have_posts()) : $my_query->the_post();
    		// Add post to exclude array
    		$excludes[] = $post->ID;
    
    			// LOOP STUFF, title, content, etc..
    
    		// End loop
    		endwhile;
    	// End if this has posts
    	endif;
    
    	// Query two
    	$my_query = new WP_Query('category_name=NAME2&posts_per_page=1');
    	// If this query has posts
    	if($my_query->have_posts()) :
    		// Loop over the posts
    		while ($my_query->have_posts()) : $my_query->the_post();
    		// Add post to exclude array
    		$excludes[] = $post->ID;
    
    			// LOOP STUFF, title, content, etc..
    
    		// End loop
    		endwhile;
    	// End if this has posts
    	endif;
    
    	$args = array(
    		'post__not_in' => $excludes
    		// You can add other args here, use this format
    		// 'parameter' => 'value'
    		// NOTE: If "value" is numeric (a number), then the single quotes are not required around the value
    	);
    
    	// Regular loop
    	query_posts( $args );
    
    	// .... if have posts, while have posts etc...
    ?>
  3. henkholland
    Member
    Posted 4 years ago #

    Yes, with something like this:

    The following will display the 5 posts which follow the most recent (1): 
    
    query_posts('posts_per_page=5&offset=1');

    from here (about halfway the page): http://codex.wordpress.org/Template_Tags/query_posts

  4. geezerd
    Member
    Posted 4 years ago #

    T, I can't seam to be able to get that working.
    Maybe because half is in header.php and the other half in index.php.
    No problem with the 2 posts displaying in the header, but it doesn't cut them out of the regular loop, which is what I need.

    All of the following is in index.php, everything above this is in header.php:

    <?php $args = array( 'post__not_in' => $excludes ); ?>
    <?php query_posts( $args ); ?>
    <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?>

    I've tried sticking <?php query_posts( $args ); ?> in a few different spots, but nothing removes those 2 posts.

  5. geezerd
    Member
    Posted 4 years ago #

    Yup, it's cuz it was split into 2 different pages. I just dumped all the header stuff into index.php, works now.

    So that "$excludes" array must get dumped out of the data base when it starts reading another .php page

    (Yah, I know crap about PHP, lol)

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    I think you only need globalise the exclude variable to access it in the index file.

    <?php
    global $excludes;
    $args = array( 'post__not_in' => $excludes );
    query_posts( $args );
    ?>
    <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?>

    Same should apply for other theme files to ... (if you wanted to load the excludes into query_posts elsewhere).

  7. geezerd
    Member
    Posted 4 years ago #

    So the beginning stays the same in the header:

    <?php
    	// Create empty array to store post ids in
    	$excludes = array();
    
    	// Query one
    	$my_query.... etc, etc
    until
    // End loop
    		endwhile;
    	// End if this has posts
    	endif; ?>  <-- add that to close the php

    Right?

    Then just use that new block of code anywhere I'd want those 2 to not show?

  8. Mark / t31os
    Moderator
    Posted 4 years ago #

    Presumably you would just update the bit in the index yes ..

    i've not tested anything though, so i have to assume either

    a) it works
    or
    b) you'll test it to see if it works

    ;) to save me testing it... ;)

    ----

    // End if this has posts
    	endif; ?>  <-- add that to close the php

    Opening/closing tags, depends on what you have in your file, but yes, do add them where necessary...

  9. geezerd
    Member
    Posted 4 years ago #

    Just tested it, Nope, doesn't work.

    It still only works if it's all in the same php page

  10. Mark / t31os
    Moderator
    Posted 4 years ago #

    You're right, doesn't seem to work... :( ...

    The header.php doesn't seem to retain set vars, where as for example the functions.php will, if you set $a = 5; , then echo in the index you'll see 5, not if you set it in the header though...

    I'm sure that use to work.. guess i'm wrong..

  11. geezerd
    Member
    Posted 4 years ago #

    Well, it doesn't really matter, since I decided I actually only want those 2 posts on the Home page, and that works, so it's all good.

    Thanks!

  12. Halibutt
    Member
    Posted 4 years ago #

    I came up with a similar problem here. Could anyone help me?
    Cheers

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.