Let's say I have a query stored in a array.

I want to parse this array, find the first posts inside this array that match a certain value from a certain key, and so extract the whole post.

Is there a elegant way to do it ? Or, is it possible to run a query_posts() inside this array ?

Thanks !


Sorry, just learning to use this fantastic website, need to learn correct use.

The purpose is to make a get_posts(), and display the posts not in a row, but at different places in the homepage where there is a place for a Beauty category post, a Mode category Post, etc. As any post could be in several categories, I cannot just ask for the last post in the category: a post could be in two places at the same time.

My idea is to just make a get_posts query, store the result, and parse it so i could take record of what post are displayed and what are not.

So, I make a query with get_posts() are store result in a variable. As I don't want certain posts to stay in the query, I use 'post__not_in', which is array with posts ID I don't want.

function request_highlights($exclude_ids) { 
/* request that will populate my hightlight feed */
$args = array(
'numberposts'   => 16,
'category'  => null,
'orderby'   => 'post_date',
'order'     => 'DESC',
'post__not_in'  => $exclude_ids,

return get_posts( $args );

$highlights_posts = request_highlights($slides_id);

So at this point, I have a variable which is a objet, I guess, with all needed data.

What I want to do is to get, say, the first post in the objet that has the cat_ID=5 (for example).

What i'm doing right now is :

foreach ($highlights_posts as $highlights_post) {
        $fetched_post = get_the_category($highlights_post->ID);
            foreach ( $fetched_post as $fetch_cat) {
                if ($fetch_cat->cat_ID == $cat_sq) {
                    $sq_post_id = $fetch_cat->cat_ID;
                    break 2;

// stuff to do like get_post($sq_post_id);

Now i realise that I would need to remove that post ID from my list $highlights_posts so I cannot pull it twice. I guess i need to learn more about arrays ?

  • A query stored in an array? Code sample please.
    – scribu
    Nov 17, 2011 at 19:49
  • @scribu Sorry, maybe i'm using wrong terms. This should be an objet. First, the query : /* requete pour peupler highlights */ $highlights_posts = request_highlights($slides_id); function request_highlights($exclude_ids) { /* la requete qui va peupler le slider */ $args = array( 'numberposts' => 16, 'category' => null, 'orderby' => 'post_date', 'order' => 'DESC', 'post__not_in' => $exclude_ids, ); return get_posts( $args ); }
    – Simon
    Nov 17, 2011 at 19:50
  • Don't post the code in a comment. Update the question.
    – scribu
    Nov 17, 2011 at 19:52
  • This question makes no sense can you post code examples and say exactly what each thing is rather than what you refer to it as, e.g. someone said the FAQ page in another question but they actually meant the page showing the faq category
    – Tom J Nowell
    Nov 17, 2011 at 19:53
  • can you translate the french and put it in your original question so we can see the formatting? Its unreadable as a comment
    – Tom J Nowell
    Nov 17, 2011 at 19:53

1 Answer 1

// store the IDs of the posts we've already displayed to prevent duplicates
$used_ids = array();

$highlights_posts = request_highlights($slides_id);
foreach( $highlights_posts as $post ) {

    // check if we've already done this post
        // skip to the next one
    // we haven't done this post before, add it's ID to the list
    $used_ids[] = $post->ID;
    // do stuff e.g. the_title(); etc

edit: Having seen your code would it not make more sense to put the category ID in the function or as a parameter instead of doing 'category' => null ? It would save you from doing the if statements and the category loop

  • Thanks, this enlights me about the use of setup_postdata() But I would to like to do so only to specific posts, not all of them.
    – Simon
    Nov 17, 2011 at 20:20
  • in that case you need an if statement just after setup_postdata that checks for some condition, such as if post is newer than 2 days do this, or if post is 500 characters long or more do this etc
    – Tom J Nowell
    Nov 17, 2011 at 20:22
  • Ok, I think that would work. But if I want to not display twice the same post, I need a way to store that i already have pulled it from the object. Is there any way to modify this object without having to send another query?
    – Simon
    Nov 17, 2011 at 20:37
  • reply to your edit : i have to think about it, this could be another good idea.
    – Simon
    Nov 17, 2011 at 20:38
  • 1
    Sorry, I didn't find the accepted answer button ! Clicked now. Thank you for your help.
    – Simon
    Nov 18, 2011 at 13:54

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.