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.

I've read several examples of how to do this but I still can't get it to work - each time I use get_posts() it seems to only keep and display the first set of results.

loop.php

function home_index() {

global $post;

echo '<ul>';

$args = array(
    'numberposts'     => 3,
    'offset'          => 0,
    'orderby'         => 'post_date',
    'order'           => 'DESC',
    'post_type'       => 'gp_news',
    'post_status'     => 'publish',
    'meta_key'        => '_thumbnail_id',
    'meta_value'      => 1,
    'meta_compare'    => '>='
);

$myposts = get_posts( $args );

foreach( $myposts as $post ) {
    setup_postdata($post);
    echo '<li>';

    if ( has_post_thumbnail() ) {
        the_post_thumbnail( 'homepage-thumbnail' );
    }
    ?>
    <h1>news <a href="<?php the_permalink(); ?>" title="Permalink to <?php esc_attr(the_title()); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    <?php the_excerpt(); ?>
    <a href="<?php the_permalink(); ?>">Continue...</a>
    <?php
    echo '</li>';
}

$args = array(
    'numberposts'     => 1,
    'offset'          => 0,
    'orderby'         => 'post_date',
    'order'           => 'DESC',
    'post_type'       => 'gp_competitions',
    'post_status'     => 'publish',
    'meta_key'        => '_thumbnail_id',
    'meta_value'      => 1,
    'meta_compare'    => '>='
);

$myposts = get_posts( $args );

foreach( $myposts as $post ) {
    setup_postdata($post);
    echo '<li>';

    if ( has_post_thumbnail() ) {
        the_post_thumbnail( 'homepage-thumbnail' );
    }
    ?>
    <h1>competition <a href="<?php the_permalink(); ?>" title="Permalink to <?php esc_attr(the_title()); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    <?php the_excerpt(); ?>
    <a href="<?php the_permalink(); ?>">Continue...</a>
    <?php
    echo '</li>';
}

echo '</ul>';   

}
share|improve this question
    
can you check your error log for apache? –  xLRDxREVENGEx May 1 '11 at 6:09
    
I tried it myself and it worked. What does a var_dump() of the $myposts variables say? –  Geert May 1 '11 at 6:14
    
The var_dump's of $myposts are all identical. Maybe it's got something to do with being in the loop.php file itself? –  EddyR May 1 '11 at 6:31
    
It doesn't show anything out of the ordinary. –  EddyR May 1 '11 at 6:31
add comment

4 Answers

I was able to query two custom post types by using this line:

AND wposts.post_type IN ('custom_post_type_1', 'custom_post_type_2')

This worked for my situation, where each custom post type was being sorted by the same meta_key

share|improve this answer
    
How did you insert this query part into the main SQL query? Please elaborate. –  kaiser Oct 25 '12 at 13:23
add comment
$args = aray(
'post_type' => array ( 'post', 'page','event')
);
query_posts($args);

Source

share|improve this answer
add comment
up vote 1 down vote accepted

So I finally figured out 2 ways of doing it. The first way is...

global $wp_query;

$args = array(
    array(
        'numberposts'     => 3,
        'offset'          => 0,
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'post_type'       => 'gp_news',
        'post_status'     => 'publish',
        'meta_key'        => '_thumbnail_id',
        'meta_value'      => 1,
        'meta_compare'    => '>='
    ),
    array(
        'numberposts'     => 1,
        'offset'          => 0,
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'post_type'       => 'gp_competitions',
        'post_status'     => 'publish'
    )
);

echo '<ul class="hp_minifeatured">';
$temp_query = clone $wp_query;
for($index = 0; $index < count($args); $index++) {
    $myposts = new WP_Query( $args );

    if($myposts->have_posts()) {
        while($myposts->have_posts()) {
            $myposts->the_post();
            echo '<li>';

            if ( has_post_thumbnail() ) {
                the_post_thumbnail( 'homepage-thumbnail' );
            }
            ?>
            <p><?php echo($args[$index]['post_type']); ?></p>
            <h1><a href="<?php the_permalink(); ?>" title="Permalink to <?php esc_attr(the_title()); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
            <?php the_excerpt(); ?>
            <a href="<?php the_permalink(); ?>">Continue...</a>
            <?php
            echo '</li>';
        }
    }
}
$wp_query = clone $temp_query;  
echo '</ul><div class="clear"></div>';

...but this results in using multiple queries.

Another way is...

global $wpdb;
global $post;

$querystr = "(SELECT wp_posts.* FROM $wpdb->posts wp_posts, $wpdb->postmeta wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id and wp_posts.post_status = 'publish' and wp_posts.post_type = 'gp_news' and wp_postmeta.meta_key = '_thumbnail_id' and wp_postmeta.meta_value >= 1 ORDER BY wp_posts.post_date DESC LIMIT 3) union (SELECT wp_posts.* FROM $wpdb->posts wp_posts, $wpdb->postmeta wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id and wp_posts.post_status = 'publish' and wp_posts.post_type = 'gp_competitions' and wp_postmeta.meta_key = '_thumbnail_id' and wp_postmeta.meta_value >= 1 ORDER BY wp_posts.post_date DESC LIMIT 1)";
$pageposts = $wpdb->get_results($querystr, OBJECT);

if ($pageposts) {
    foreach ($pageposts as $post) {
        setup_postdata($post);
        echo '<li>';
        if ( has_post_thumbnail() ) {
            the_post_thumbnail( 'homepage-thumbnail' );
        }
        ?>
        <h1><a href="<?php the_permalink(); ?>" title="Permalink to <?php esc_attr(the_title()); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
        <?php the_excerpt(); ?>
        <a href="<?php the_permalink(); ?>">Continue...</a>
        <?php
        echo '</li>';
    }
}
share|improve this answer
add comment

You shouldn't have to do all that. The below function will allow you to control what shows up in the default loop without having to rebuild the query itself

function cpt_pre_get_posts_filter( $query ) {
  global $wp_query;

  if ( !is_preview() && !is_admin() && !is_singular() && !is_404() ) {
    if ($query->is_feed) {
    /* set your post types in the array below */
    } else {
      $my_post_type = get_query_var( 'post_type' );
      if ( empty( $my_post_type ) )
        $query->set( 'post_type' , 'my-post-type', 'my-other-post-type' );
    }
  }

  return $query;
}
add_filter( 'pre_get_posts' , 'cpt_pre_get_posts_filter' );
share|improve this answer
    
I don't think that will achieve what I want. I want each post type shown in that order, not mixed together. And you'll notice the first post type shows 3 post and all the rest just show 1. –  EddyR May 3 '11 at 8:35
add comment

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.