Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Currently all the below bit of code is doing is showing 3 random posts from the same category that the current page resides in. So if the current page falls in the artists category, for example, show 3 random posts from the artists category down the side of the page. The problem I have is that the current post keeps appearing within this section, I'd like to exclude it but can't work out how to do this using my current loop. Here's my loop:

<?php if ( in_category( 'artists' ) ) {  ?>
                <h2>Similar articles</h2>
                <?php // Display the 3 random posts from page category
                query_posts('showposts=3&cat=1,-12&orderby=rand'); ?>

        <?php } ?>

        <?php if ( in_category( 'people' ) ) {  ?>
                <h2>Similar articles</h2>
                <?php // Display the 3 random posts from page category
                query_posts('showposts=3&cat=3&orderby=rand'); ?>

        <?php } ?>

        <?php if ( in_category( 'development' ) ) {  ?>
                <h2>Similar articles</h2>
                <?php // Display the 3 random posts from page category
                query_posts('showposts=3&cat=5&orderby=rand'); ?>

        <?php } ?>

        <?php if ( in_category( 'offsite' ) ) {  ?>
                <h2>Similar articles</h2>
                <?php // Display the 3 random posts from page category
                query_posts('showposts=3&cat=7&orderby=rand'); ?>

        <?php } ?>

        <?php if ( in_category( 'projects' ) ) {  ?>
                <h2>Similar articles</h2>
                <?php // Display the 3 random posts from page category
                query_posts('showposts=3&cat=6&orderby=rand'); ?>

        <?php } ?>

        <?php if ( in_category( 'contact' ) ) : // Display nothing ?>

            <?php // Else, show boxes
            else : ?>

                <?php while (have_posts()) : the_post(); ?>

                    <?php foreach( get_the_category() as $cat ) echo '<div class="module ' . $cat->slug . '" data-category="' . $cat->slug . '" >'; ?>

                          <a href="<?php the_permalink()?>" title="<?php the_title(); ?>">
                          <div class="active">
                                <div class="hover">    </div>
                                <?php the_post_thumbnail(); ?>
                          </div>

                         <?php
                              $sub_title=get_post_meta($post->ID,'subtitle',true);
                              if($sub_title != '') {
                              echo '<h1>'. get_the_title() .'<span> / '. $sub_title .'</span></h1>';
                              } else {
                              echo '<h1>'. get_the_title() .'</h1>';
                              }
                              ?>

                              <?php
                              // Call in the contents of a custom field called Excerpt and if custom field in admin panel is empty don't display <p> tags otherwise wrap contents in <p> tags
                              $excerpt=get_post_meta($post->ID,'Excerpt',true);
                              if($excerpt != '') {
                              echo '<p>'. $excerpt .'</p>';
                              } else {
                              echo ' ';
                              }
                              ?>
                              <p class="date"><?php the_time('YdmHi') ?></p>
                         </a>
                    </div>

                <?php endwhile;?>

        <?php endif; ?>
share|improve this question
2  
First thing is don't use query_posts to query the posts. if you can describe what you are trying to achieve, someone might be able give you better alternative. –  Hameedullah Khan May 31 '12 at 11:41

2 Answers 2

up vote 1 down vote accepted

First, do not use query_posts() for secondary loops. The query_posts() function is intended only to modify the primary loop query. Use WP_Query() or get_posts() for secondary loop queries.

Also, showposts is deprecated. Use posts_per_page instead.

Let's use WP_Query(), as it will be the most analogous to your current implementation:

<?php
// First, let's eliminate some DRY,
// by making an array of our categories
$random_posts_cat_array = array( 'people', 'development', 'offsite', 'contact' );

// Globalize $post,
// since we're outside the primary loop
global $post;
$post_cats = get_the_category( $post->ID );
// First array object
$post_cat = $post_cats[0];
// Current post category ID
$post_cat_id = $post_cat->term_id;
// Current post category slug
$post_cat_slug = $post_cat->slug;

// Now, let's find out if we're displaying
// the category index for one of our categories
if ( in_array( $post_cat_slug, $random_posts_cat_array ) ) {

    // Set up custom loop args
    $random_posts_query_args = array(
        // Only 3 posts
        'posts_per_page' => 3,
        // Ordered randomly
        'orderby'       => 'rand',
        // Exclude current post
        'post__not_in'  => array( $post->ID )
    );
    // Add Cat ID to custom loop args
    foreach ( $random_posts_cat_array as $random_post_cat ) {
        if ( $post_cat_slug == $random_post_cat ) {
            // Add Cat ID
            $random_posts_query_args['cat'] = $post_cat_id; 
        }
    }

    // Run random posts query
    $random_posts_query = new WP_Query( $random_posts_query_args );

    // Setup random posts query loop
    if ( $random_posts_query->have_posts() ) : while ( $random_posts_query->have_posts() ) : $random_posts_query->the_post();
        // Loop output goes here
    endwhile; endif;

    // Be kind; rewind
    wp_reset_postdata();

} else {
    // Alternate output goes here, if any
}
share|improve this answer
    
Thanks Chip, although its not working. I've broken up your PHP to include my HTML structure for the output, see new code here: pastebin.com/aMhYPuVZ What am I doing wrong? –  egr103 May 31 '12 at 15:01
    
What's "not working"? Are you getting a syntax error? Unexpected query object data? Etc. –  Chip Bennett May 31 '12 at 15:11
    
Well no posts appear at all. Not sure how else to test PHP other than testing how it appears in the browser. After checking the source though, my main loop (on single.php) stops and my footer.php isn't loaded into the document. –  egr103 May 31 '12 at 15:19
    
You're getting some sort of PHP fatal error. Enable WP_DEBUG and report back any errors. –  Chip Bennett May 31 '12 at 15:23
    
I found and fixed a couple PHP syntax errors. –  Chip Bennett May 31 '12 at 15:25

I think you need to us the 'post__not_in' parameter. eg your first query would be something like this:

query_posts(array('showposts'=>3,'cat'=>array(1,-12),'orderby'=>'rand',
'post__not_in' => array($id)));

You'll need to add a global $id; there as well.

share|improve this answer
    
What do you mean by the global $id;? How do I do that? –  egr103 May 31 '12 at 14:05
    
Add it to the top of your function. The global variable $id should be the id of the current post. –  Bruce Pearson May 31 '12 at 14:09
    
-1 for using query_posts(). Change the method to WP_Query() and I'll change that to a +1. :) –  Chip Bennett May 31 '12 at 14:10
    
Your right, it should probably be WP_Query(). But that wasn't the question asked. –  Bruce Pearson May 31 '12 at 14:15
    
So to confirm my artists code above would like this: <?php $id=$post->ID; query_posts(array('showposts'=>3, 'cat'=>array(1,-12), 'orderby'=>'rand','post__not_in' => array($id))); ?> –  egr103 May 31 '12 at 14:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.