• Resolved shagdirty

    (@shagdirty)


    I’m trying to use query_posts and post__in to display posts from a dynamically generated array. I’ve got the array working great and can echo it anywhere on the page that I want. (it echoes via implode as: 543, 234, 433 etc. which are the desired post IDs). When I try to throw the array in a variable and use that variable within post__in it doesn’t work as expected. Here’s what I’m using…

    $horse_array = implode(', ', $horse_var);
    echo $horse_array; // this echoes: 546, 540, 550
    
    <?php query_posts( array( 'post_type' => 'client_horses', 'orderby' => 'title', 'order' => 'asc', 'horse-status' => 'mare', 'post__in' => array($horse_array) ) ); ?>
    
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
    MY LOOP STUFF... (only displays the last post in the array)
    
    <?php endwhile; ?>
    <?php endif; ?>
    
    <?php wp_reset_query(); ?>

    So this will show me the last item in the array but ONLY the last item. I would expect it to just show all the items within the array but nope. I know that the array has more than one item in it since it echoes multiple items just before (and after) my query. I also tried imploding the array within the post__in param but that didn’t work either.

    Thoughts anyone?

Viewing 5 replies - 1 through 5 (of 5 total)
  • shouldn’t you be using your array $horse_var in the query?

    to check array content, for instance, you can use:

    <?php var_dump($horse_var); ?>

    Thread Starter shagdirty

    (@shagdirty)

    Just tried using $horse_var in place of $horse_array but to no avail. If I do that, I get nothing back, not even the last item in the array. I get this when I dump horse_var:

    array(3) { [0]=> string(3) "546" [1]=> string(3) "540" [2]=> string(3) "550" }

    My logic (as ignorant and flawed as it may be) was to try and echo a string of post ID numbers. if I echo $horse_var I just get “Array” so I didn’t think that would work. I’m terrible with arrays so I’ve struggled pretty hard just to get to this point!

    arrays can be confusing, indeed.
    my mistake, i didn’t check this in my first reply
    – ‘post__in’ uses an array, but when ‘$horse_var’ is already an array, then the word ‘array’ must not be used:

    this should hopefully work:
    <?php query_posts( array( 'post_type' => 'client_horses', 'orderby' => 'title', 'order' => 'asc', 'horse-status' => 'mare', 'post__in' => $horse_var ) ); ?>

    explanation:
    ok:
    'post__in' => array(3 ,5, 66)

    not ok:

    $horse_var = array(3 ,5, 66);
    'post__in' => array($horse_var)

    ok:

    $horse_var = array(3 ,5, 66);
    'post__in' => $horse_var

    Thread Starter shagdirty

    (@shagdirty)

    WOOOOO! (in my Nature Boy Rick Flair voice). Worked like a charm! Thanks a million. Arrays become slightly less mystified to me.

    This raises another question, (perhaps for another post but since I have the attention of a savvy individual) If there’s nothing within post__in, query_posts returns everything I guess? that’s what my initial test shows me anyway. Is there a way around that off the top of your head? So if an array is empty will it return false so I can use some conditional logic or something?

    if $horse_var {
    // spit out the array in a different var maybe?
    } else {
    // nothing in the array so show a default post ID maybe???
    }

    Just so you know, $horse_var comes from here…

    $horse_var = get_the_author_meta( 'horses', $current_user->ID );

    So basically if the current user has horses associated with it, display those horses. If the current user has NO horses, I don’t want them to see ALL the horses. Clear as mud?

    thanks

    Thread Starter shagdirty

    (@shagdirty)

    Actually I think I’ve found the solution via a search…

    if(empty($horse_var)){
       echo "horse_var is false";
    }else{
       echo "horse_var is true";
    }

    I’ll probably just wrap my whole query in this logic which should do the trick, albeit in a less than efficient fashion I’m sure.

    Rebuttal anyone?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘using a dynamic array with query_posts and post__in’ is closed to new replies.