WordPress.org

Forums

[resolved] Using an array in query_posts (2 posts)

  1. robahas
    Member
    Posted 2 years ago #

    Hi - I am using the following to generate a list of posts that have certain tags:

    $query = array('cat' => '4', 'orderby' => 'rand', 'tag__in' => array(7,9))

    This works great.

    The problem is that I can't seem to generate the array for tag__in dynamically. If I use an array of values ( 'tag__in' => $array )it always outputs thus:

    [tag__and] => Array ( [0] => 7,9 )

    But the query wants only values separated by commas. Because of this it only recognizes the first tag.

    So how do I make an array only output comma separated values without keys? (PS I tried implode() and got the same result).

    Thanks!

  2. robahas
    Member
    Posted 2 years ago #

    Here's how I got it working:

    $tags = '1,2,3';
    $tags = explode(',',$tags);
    $post_query = array('cat' => '4', 'orderby' => 'rand', 'tag__in' => $tags );
    query_posts($post_query);

    Here's what $post_query looks like:

    Array ( [cat] => 4 [orderby] => rand [tag__in] => Array ( [0] => 7 [1] => 9 ) )

    Maybe it will help someone.

Topic Closed

This topic has been closed to new replies.

About this Topic