Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to use the search query along with an array of arguments to narrow down search results, but I'm failing horribly. This is what I have so far.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;          

$query_string = 's=test&category=wordpress'      

$s_array = array(
    'post_type' => 'blog',
    'caller_get_posts' => 1, 
    'paged' => $paged, 
    'meta_query' => array(
        array(
            'key' => 'votes_percent',
            'value' => '50',
            'compare' => '>',
            'type' => 'numeric',
        )
    )
);
$s_query = http_build_query($s_array);
$is_query = '&' . $s_query;
$s_streaming = $query_string . $is_query;  
query_posts($s_streaming);

When echoing out $s_streaming I get

s=test&category=wordpress&post_type=blog&caller_get_posts=1&paged=1&meta_query%5B0%5D%5Bkey%5D=votes_percent&meta_query%5B0%5D%5Bvalue%5D=50&meta_query%5B0%5D%5Bcompare%5D=%3E&meta_query%5B0%5D%5Btype%5D=numeric

If I remove the meta_query keys it works, so I'm guessing that is where my problem resides.

It works fine like this

query_posts($s_array); //just using the array to filter

query_posts($query_string); //just using the search query
//$query_string = 's=test&category=wordpress';

I'm trying to build the string to query, because this fails.

query_posts($query_string . $s_array); //using both

Can anyone point me in the right direction?

share|improve this question
    
Probably because $s_array is an array, whereas $query_string is a string. I'm sure you cannot append these two. Can you edit your code to include what $query_string holds? –  Rutwick Gangurde Oct 24 '11 at 4:36
    
Updated. That is what I thought to, which is why I convert the $s_array to a string then tried appending it. But the meta_query keys are held in another array which is what I think the problem is. –  TimothyJr Oct 24 '11 at 16:36
    
Can you show $query_string value? –  Rutwick Gangurde Oct 24 '11 at 16:47
    
Sorry, Updated the question again with it's value which is 's=test&category=wordpress' –  TimothyJr Oct 24 '11 at 16:57
    
was looking for the same thing... and this function will do it! codex.wordpress.org/Function_Reference/wp_parse_args –  user23700 Nov 16 '12 at 1:24
add comment

2 Answers

up vote 2 down vote accepted

I'd suggest not using $query_string to simplify things. If you're using an array, stick with the array form for the query variables:

global $wp;
$paged = ((int)get_query_var('paged')) ? (int)get_query_var('paged') : 1;
$s_array = array(
    'post_type' => 'blog',
    'caller_get_posts' => 1,
    'paged' => $paged,
    'meta_query' => array(
        array(
            'key' => 'votes_percent',
            'value' => '50',
            'compare' => '>',
            'type' => 'numeric',
        )
    )
);
$new_query = array_merge( $s_array, (array)$wp->query_vars );
query_posts($new_query);
share|improve this answer
    
this works, but is ignoring the category key/value pair. –  TimothyJr Oct 24 '11 at 17:36
    
ok it must have been a flaw inside my code, because its working now. Thanks! –  TimothyJr Oct 24 '11 at 17:40
add comment

If you're trying to combine $query_string and $s_array, try this...

<?php

    //We use values of 's' and 'category' to add values to $s_array
    $query_string = 's=test&category=wordpress';

    $s_array = array(
        'post_type' => 'blog',
        'caller_get_posts' => 1, 
        'paged' => $paged, 
        'meta_query' => array(
        array(
            'key' => 'votes_percent',
            'value' => '50',
            'compare' => '>',
            'type' => 'numeric',
            )
        ),
        's' => 'test',
        'category' => 'wordpress'
    );

    //Use $s_array for query_posts directly
    query_posts($s_array);
?>

I guess this will work...

share|improve this answer
    
I thought about this, but I generate the $query_string threw a search form. any way to append to an array? Also the user has the option to not include some fields. Like category sometimes won't be declared. –  TimothyJr Oct 24 '11 at 17:25
    
@TimothyJr My answer takes the variable nature of the page's query into account above and keeps everything typed as an array. –  John P Bloch Oct 24 '11 at 17:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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