0

I have a query that pulls custom post types and regular posts, but how could I tell it to offset just one post type in the query?

My query:

<?php query_posts( array(
      'posts_per_page' => 10,
      'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
 ));
?>

But I want it to include:

If 'Post Type' exists, offset by 1.

Any help is greatly appreciated!

2
  • What exactly do you mean by "offset by 1"? Offset the query_posts by 1 post? And also, why wouldn't the post type exist if you declared it? Commented Nov 7, 2011 at 20:27
  • The query above includes regular posts on various other post types. I want to be able to offset one particular post type out of the query.
    – Erika
    Commented Nov 7, 2011 at 21:07

3 Answers 3

2

What you're trying to do is not going to be able to be done via a single query. Not sure why you want to get all posts of all post types.

But, in order to do what you want, you will have to just have php logic check if it is the first post in that post type and if so, skip it.

3
  • I'll do my best to explain.
    – Erika
    Commented Nov 9, 2011 at 17:06
  • My site is for an online radio show. I have 2 queries on the homepage. The first query grabs the newest episode information and is displayed at the top of the page along with the video player for the live show. The second queries everything else which includes a custom post type called 'stories', past episodes and regular posts. As it is now, when I post a new episode is shows up perfectly at the top of the page, but is duplicated in the second query. I don't want the newest episode post type to show up in the second query but I do want past episodes to appear. Make sense?
    – Erika
    Commented Nov 9, 2011 at 17:12
  • based on this explanation, your best solution is to just pass in the post_id of the newest episode as an 'exclude' argument to your second query. Commented Jan 21, 2012 at 13:57
0

Not quite sure what you mean by If 'Post Type' exists but could you do something like:

<?php 
    query_posts( 
        array( 
            'posts_per_page' => 10, 
            'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ), 
            if("Post Type exists?"){ 'offset' => 1 }
    )); 

? >
1
  • This didn't work unfortunately. :(
    – Erika
    Commented Nov 9, 2011 at 17:05
0

As Kevin Langley Jr. says, you'll probably need to use php logic to do this.

You'll need to modify this for you query but in your loop try adding a flag for the post type:

$firstOfPostType = true; //set your flag to true initially

if( have_posts() ) : while( have_posts() ) : the_post();
if( $firstOfPostType == true && $post->post_type == "YOUR POST TYPE" ){
    continue;// Skip this iteration
    $firstOfPostType = false;// set this to false so it doesn't run again.
}

[edit: to add formatted code as supplied in comments]

This may work. Depending on what's in content-POSTFORMAT.php which is brought in by get_template_part() you may need to put the code in there.

<?php 

query_posts( 
array( 
    'posts_per_page' => 10, 
    'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ), 
    )
); 


$posts = get_posts( 
    array ( 
        'numberposts' => '10', 
        'post_type' => array ( 
            'post', 
            'episodes', 
            'featured-stories' 
        ), 
    ))
; 
$firstOfPostType = true;
foreach ($posts as $post) : start_wp(); 

if( $firstOfPostType == true && $post->post_type == "episodes"){ 
    $firstOfPostType = false;
    continue;
}

?> 


<?php 
    static $count2 = 0; 
    if ($count2 == "10") { break; } 
    else { ?> 
        <?php get_template_part( 'content', get_post_format() ); ?> 
        <?php $count2++; } ?> 
<?php endforeach; ?> 
3
  • Not sure how to incoporate that with the loops I have on my page:
    – Erika
    Commented Nov 10, 2011 at 0:33
  • <?php query_posts( array( 'posts_per_page' => 10, 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ), )); ?> <?php $posts = get_posts( array ( 'numberposts' => '10', 'post_type' => array ( 'post', 'episodes', 'featured-stories' ), )); foreach ($posts as $post) : start_wp(); ?> <?php static $count2 = 0; if ($count2 == "10") { break; } else { ?> <?php get_template_part( 'content', get_post_format() ); ?> <?php $count2++; } ?> <?php endforeach; ?>
    – Erika
    Commented Nov 10, 2011 at 0:35
  • I've put code in the question above. I think this should work though not all your code is there because there's an include (get_template_part()). The loop on your get_posts() query is a bit odd too. I'm pretty sure wp-start() is deprecated & you shouldn't need the counter in your loop since you have numberposts in your query... but if it's working...
    – patnz
    Commented Nov 10, 2011 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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