Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Query Multiple Custom Post Types WordPress

  • SOLVED

Hello,
I am making tabbed widget on my blog, in sidebar there are tabbed widget where are popular, reviews, media and blog [[LINK href="http://milanchymcak.com/"]]http://milanchymcak.com/[[/LINK]], well I have custom post types reviews, so I query them like this

$recentPosts->query('post_type=reviews&showposts=8');
Well, now there is problem that I want to fix. I want to query for media tab videos and photos.
So I tried this
$recentPosts->query('post_type=videos&post_type=photos&showposts=8');
But this doesnt work at all. IT shows only posts from videos and not photos.

What I should do ? thank you,
Milan Chymcak

Answers (1)

2013-06-30

Yakir Sitbon answers:

$query = new WP_Query( array(
'post_type' => array( 'videos', 'photos' ),
'posts_per_page' => 8,
) );


Chymmi comments:

It doesn't work or I don't know how to apply it :(

Well there is whole code

echo '<div id="tabs-3" class="tab tab-recent">';
echo '<ul>';

$recentPosts = new WP_Query();
$recentPosts->query('post_type=videos&post_type=photos&showposts=8');
while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<li class="clearfix">
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { ?>
<div class="tab-thumb">
<a href="<?php the_permalink();?>" class="thumb" target="_blank"><?php the_post_thumbnail(); ?></a>
</div>
<?php } ?>
<h5 class="entry-title"><a target="_blank" href="<?php the_permalink(); ?>" class="title"><?php the_title();?></a></h5>
<div class="entry-meta entry-header">
<span class="published"><?php the_time( get_option('date_format') ); ?></span>
<span class="meta-sep">&middot;</span>
<span class="comment-count"><a href="<?php comments_link(); ?>" target="_blank"><?php comments_number('No Comments', '1 Comment', '% Comments'); ?></a></span>
</div>
</li>
<?php endwhile;

echo '</ul>';
echo '</div>';


Yakir Sitbon comments:

<?php
echo '<div id="tabs-3" class="tab tab-recent">';
echo '<ul>';
$recentPosts = new WP_Query( array(

'post_type' => array( 'videos', 'photos' ),

'posts_per_page' => 8,

) );
while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<li class="clearfix">
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { ?>
<div class="tab-thumb">
<a href="<?php the_permalink();?>" class="thumb" target="_blank"><?php the_post_thumbnail(); ?></a>
</div>
<?php } ?>
<h5 class="entry-title"><a target="_blank" href="<?php the_permalink(); ?>" class="title"><?php the_title();?></a></h5>
<div class="entry-meta entry-header">
<span class="published"><?php the_time( get_option('date_format') ); ?></span>
<span class="meta-sep">&middot;</span>
<span class="comment-count"><a href="<?php comments_link(); ?>" target="_blank"><?php comments_number('No Comments', '1 Comment', '% Comments'); ?></a></span>
</div>
</li>
<?php endwhile;
echo '</ul>';
echo '</div>';


Chymmi comments:

Nice that works

$recentPosts = new WP_Query( array(

'post_type' => array( 'videos', 'photos' ),

'posts_per_page' => 8,

) );


Thanks