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.

How can I query all the posts from either the custom post type ('videos') or with a post category ('video') in a loop?

I've managed to create a query that combines the posts from a custom post type and the normal posts using the code below, but am struggling with achieving the same with a custom post type and a post category.

<?php $args = array_merge( $wp_query->query, array( 'post_type' => array( 'post', 'videos', ) ) ); query_posts( $args ); ?>     
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

Any help would be appreciated!

This is what my slides look like:

if($query->have_posts()){
while ( $query->have_posts() ) {
    $query->the_post();
    $carousel_slide = '<li>
               <a href="<?php the_permalink(); ?>">
               <div class="tile-image"><?php the_post_thumbnail('tile-small-thumb'); ?></div>
               <div class="tile-up-arrow"></div>
               <div class="videoslider">Video</div>
               <div class="tile-post-title">
                   <h5><?php the_titlesmall('', '...', true, 55); ?></h5>
               </div>
               <div class="hover-display" ></div>
               </a>
           </li>';
    $found_posts[] = $post->ID;
    $slides[] = $carousel_slide;
}
wp_reset_postdata();

}

First off, I get a Parse error: syntax error, unexpected T_STRING on <?php the_post_thumbnail('tile-small-thumb'); ?> and a Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING on <h5><?php the_titlesmall('', '...', true, 55); ?></h5>. Removing these loads the page fine, but doesn't render the links as links, but instead as <?php the_permalink(); ?>.

If I'm just doing everything wrong here please let me know and I'll go ahead and close this question.

Thanks again for you time.

share|improve this question
    
I'd suggest you use WP_Query instead of query_posts, but can you explain what the end result of this would be and what you're trying to achieve by doing this? It's a little hard to picture, any kind of clarification would be enormously helpful –  Tom J Nowell Jan 16 '13 at 15:57
    
The above query is being displayed in a jcarousel which would ideally display all videos on the site. The issue being video content is being added either using the custom post type (in house created video content), or as a normal blog post with the category "video" (with external content). Would you mind giving an example on the WP_Query you had in mind? –  kallekillen Jan 16 '13 at 16:01
    
So you want to get posts that are of the post type 'videos' OR in the category 'videos'? –  Tom J Nowell Jan 16 '13 at 16:49
1  
Here's the definitive guide on how to do queries in WordPress slideshare.net/andrewnacin/… , avoid query_posts at every opportunity, and instead standardise when possible on WP_Query. –  Tom J Nowell Jan 16 '13 at 16:50
1  
I edited both your question to reflect the comments you just made and my response should provide with what you need. See the Either this or That section. That said, really look at the link @TomJNowell posted. It is vital when creating queries. Also take a look at the link in my post to WP_Query to see what options are available. –  Derk-Jan Jan 16 '13 at 17:10
show 1 more comment

2 Answers 2

up vote 3 down vote accepted

Must have both

The following arguments array searches for the post-type-slug videos and category-slug video. It doesn't use pagination by setting posts_per_page to -1 and only returns published posts.

$args = array(
    'post_type' => 'videos', 
    'category_name' => 'video', 
    'posts_per_page' => -1,
    'post_status' => 'publish'
  );

Either this or that

If you are trying to show posts with EITHER the post-type OR the category slug, use two queries, because they are NOT combinable by any means of the arguments array.

$args = array(
    'post_type' => 'videos', 
    'posts_per_page' => -1,
    'post_status' => 'publish'
  );

$args = array(
    'category_name' => 'video',
    'posts_per_page' => -1,
    'post_status' => 'publish'
  );

However, I am noticing that you are using query_posts(). If you are trying to alter the main loop, please don't. Use the pre_get_posts hook instead. If you are querying this for a secondary loop, use WP_Query instead.

With WP_Query

function create_slide() {
     return '<li>
           <a href="'. esc_attr( esc_url( get_the_permalink() ) ) .'">
           <div class="tile-image">' . get_the_post_thumbnail('tile-small-thumb') . '</div>
           <div class="tile-up-arrow"></div>
           <div class="videoslider">Video</div>
           <div class="tile-post-title">
               <h5>' . get_the_title() . '</h5>
           </div>
           <div class="hover-display"></div>
           </a>
       </li>';
}

$slides = array();
$displayed = array();
$args = array(
    'post_type' => 'videos', 
    'posts_per_page' => -1,
    'post_status' => 'publish'
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
    $displayed[] = get_the_ID();
    $slides[] = create_slide();
endwhile;

$args = array(
    'category_name' => 'video',
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'post__not_in' =>  $displayed,
);

$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
    $displayed[] = get_the_ID();
    $slides[] = create_slide();
endwhile;

wp_reset_postdata();

echo implode('', $slides);
share|improve this answer
    
Thanks for the above! However, trying to implement it resulted in a white page load for me... Please ignore my complete lack of query skills, I'm still learning. In regards to query_posts(), I'm redesigning a site that had previously been built by someone else. I will read up on WP_Query and fix this, thanks. –  kallekillen Jan 16 '13 at 17:11
    
I actually did not test it - just wrote it here, maybe I made a typo, but a white page usually means an error. Let me try it. –  Derk-Jan Jan 16 '13 at 17:18
1  
turn on error logging/reporting and let us know what the error was, a white screen of death is just an error that isn't being displayed –  Tom J Nowell Jan 16 '13 at 17:18
1  
The args arrays do not have closing semicolons –  Tom J Nowell Jan 16 '13 at 17:19
    
Thanks @TomJNowell! Just caught it when you posted that :) –  Derk-Jan Jan 16 '13 at 17:20
show 5 more comments

To do an OR rather than an AND, you would need custom logic, and it would be a hassle, but an unnecessary one!

Instead of trying to get the posts AND the category, you should do 2 individual queries to grab what you want, so grab all the video posts, put them in an array, then grab all the posts in the video category, add those to the same array, then print out what you've got.

Kind of like putting oranges in your shopping cart, then putting bannanas in, rather than looking endlessly for a box that has both oranges and bannanas together

$slides = array();
$found_posts = array();
// get the video posts
$query = new WP_Query(
    array(
        'post_type' => 'videos',
        'posts_per_page' => -1
    )
);
if($query->have_posts()){
    while ( $query->have_posts() ) {
        $query->the_post();
        $carousel_slide = 'put your slide content here, so probably an <li> element or something?';
        $found_posts[] = $post->ID;
        $slides[] = $carousel_slide;
    }
    wp_reset_postdata();
}

// get the video category
$query = new WP_Query(
    array(
        'category_name' => 'video',
        'posts_per_page' => -1
        'post__not_in' =>  $found_posts
    )
);
if($query->have_posts()){
    while ( $query->have_posts() ) {
        $query->the_post();
        $carousel_slide = 'put your slide content here, so probably an <li> element or something?';
        $slides[] = $carousel_slide;
    }

    wp_reset_postdata();
}

// we should now have all our slides in the $slides array so lets print them out

echo implode('',$slides);

You will need to modify this line which appears twice:

$carousel_slide = 'put your slide content here, so probably an <li> element or something?';

So that it contains the slide html for the post, so I imagine it will likely be an li element, some calls to grab the featured image, etc but that's beyond the scope of this question ( which is regarding queries, not how to setup JCarousel slides and featured images ).

share|improve this answer
    
I know Derk-Jan said this wouldn't be possible, but wanted to give the above a try. I'm once again met with a white screen, would you mind wrapping this in tags so I can just paste it in (sorry I know I'm asking you to spell it out for me)? –  kallekillen Jan 16 '13 at 18:01
2  
you mean you want me to write <?php and ?> at the start and end? Could you not just do that yourself? Keep in mind that you still need to modify what $carousel_slide contains, else you'll just get a tonne of 'put your slide content here.... We don't know your slide format, nor do is it in the scope of this question –  Tom J Nowell Jan 16 '13 at 18:09
1  
And again, we need to know what the error is, turn on error logging/reporting, you'll find many a question/answer on how to do this in here, and on google, look up the WP_DEBUG definition –  Tom J Nowell Jan 16 '13 at 18:10
1  
Also I had a look and I don't see where Derk said it wouldn't be possible –  Tom J Nowell Jan 16 '13 at 18:31
1  
I said it's currently not possible to combine the OR statement in a single WP_Query fetch. The problem is solvable. Both my and Toms solutions should do the trick. –  Derk-Jan Jan 16 '13 at 19:30
show 3 more comments

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.