0

I'm trying to figure out the best way to go about this and I have part of my solution, but I can't figure out all of it.

My ultimate goal is within a certain page template, to show the newest post from a CPT of a particular taxonomy, this seems to be the easiest and accomplished with this:

$args=array(
    'post_type' => 'portfolio',
    'post_status' => 'publish',
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => 1,
    'type' => 'featured'
);

$portfolio_query = new WP_Query($args);

The next part would be show all of other posts, with the exception of the post that is queried above.

Can this be done with a single loop, or will I need multiple loops with offset logic? I'm lost on how to accomplish this. TIA.

2 Answers 2

2

This will most likely require two loops to the best of my knowledge. The second loop just needs to know to exclude the post you just queried. Something like the following should do the trick.

$first_id = 5; // This should be set in the previous loop to the post ID of the post returned by your first query.
$args2 = array(
    'post_type' => 'portfolio',
    'post_status' => 'publish',
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => 10,
    'type' => 'featured',
    'post__not_in' => array( $first_id ),
);
$portfolio_query_2 = new WP_Query( $args2 );

This info is taken straight from the WP_Query page in the WP Codex.

2
  • So by using that, I'm going to need to figure out a way to get the post id from the first loop, store it as a global variable of sorts, then pass it to that second loop. Any suggestions on doing that?
    – NW Tech
    Feb 3, 2014 at 19:34
  • I used a slightly modified version of yours, by getting the post ID of the first loop, inside the loop, then passing it off to the second query.
    – NW Tech
    Feb 4, 2014 at 18:24
0

getting portfolio post type item

$arg1 = array(
            'post_type' => 'portfolio',
            'post_status' => 'publish',
            'orderby' => 'date',
            'order' => 'DESC',
            'posts_per_page' => 1,
            'type' => 'featured'
    );
    $post_res1 = query_posts($arg1);

getting all posts

$post_res2 = query_posts($arg2);

merging all results //only unique posts

$all_posts = array_merge($post_res1, $post_res2);

get all post ids from array merged

$post_ids = wp_list_pluck($all_posts, 'ID');

getting all post result

$args = array(
            'post__in' => $post_ids,
            'posts_per_page' => -1,
        );
        query_posts($args);
2
  • That looks like it includes all posts from both queries, when in fact, I need to exclude the post from the first query from the second query. Does that make sense?
    – NW Tech
    Feb 3, 2014 at 21:58
  • use array_diff instead of array_merge
    – Haris
    Feb 4, 2014 at 6:57

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.