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 have a custom search page via which I select:

One or ALL categories
One or ALL tags
One or ALL authors
A search string or no search string

On the results page, wp_query works when I have selected values. But how do I instruct to set some param as ALL? How do I set "search ALL categories" for example?

Maybe I need to break my args and add to them only when I got values? But how would I do that?

Thanks.

EDIT

I modified the wp-query and now stands as follows. All works great but I do not have paging!

$args = array( 'order' => 'DESC', 'orderby' => 'date',  'posts_per_page'=> 10 ); 
if ( '0' != $category ) { $args['cat'] = $category; } 
if ( '0' != $tag ) { $args['tag'] = $tag; } 
if ( '0' != $author ) { $args['author'] = $author; } 
if ( '' != $search ) { $args['s'] = $search; } 
$the_query = new WP_Query( $args );
while ($the_query-> have_posts()) : $the_query->the_post();
....
endwhile;
wp_pagenavi( array( 'query' => $the_query ) );
share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

Assuming you have variables to hold your custom values for each:

  • $category
  • $tag
  • $author
  • $search

Then just create an arguments array, and add keys to it conditionally:

$custom_query_args = array(
    // Set any default args here 
    // NOTE: This is where you'd add
    // things like pagination (posts_per_page),
    // order/sort parameters, and the like
    'posts_per_page' => 10,
    'orderby' => 'date',
    'order' => 'DESC'
);

// If user defines $category, add it
if ( '' != $category ) {
    $custom_query_args['cat'] = $category;
}
// If user defines $tag, add it
if ( '' != $tag ) {
    $custom_query_args['tag'] = $tag;
}

// If user defines $author, add it
if ( '' != $author ) {
    $custom_query_args['author'] = $author;
}

// If user defines $search, add it
if ( '' != $search ) {
    $custom_query_args['s'] = $search;
}

// Run the query
$custom_query = new WP_Query( $custom_query_args );

Note: how you define $category, $tag, $author, and $search is outside the scope of this answer.

Edit

If you want to include a meta query (i.e. custom field) parameter, you can do that as well:

Assuming (user-defined) $post_meta_key and (static) $post_meta_key:

// If user defines $post_meta_value, add it
if ( '' != $post_meta_value ) {
    $custom_query_args['meta_query'] = array(
        array(
            'key' => $post_meta_key,
            'value' => $post_meta_value,
            'compare' => '=',
        )
    );
}
share|improve this answer
    
This is very practical and very precise, many thanks. Could I extend this question as per how to optionally narrow my search on a specific custom field? I know I could add this to the args[] as well but how? –  p.a. Nov 4 '12 at 5:23
    
I followed your code, it all works OK but I get max 10 results and never any paging. SOmethings missing? $args = array( 'order' => 'DESC', 'orderby' => 'date' ); // If user defines $category, add it if ( '0' != $category ) { $args['cat'] = $category; } // If user defines $tag, add it if ( '0' != $tag ) { $args['tag'] = $tag; } // If user defines $author, add it if ( '0' != $author ) { $args['author'] = $author; } if ( '' != $search ) { $args['s'] = $search; } $the_query = new WP_Query( $args ); –  p.a. Nov 4 '12 at 9:42
    
I'll be happy to add that to the answer, but please edit the question to include the new information. Thanks :) –  Chip Bennett Nov 4 '12 at 13:36
    
I modfied the question. Thanks a lot in advance. –  p.a. Nov 5 '12 at 7:55
1  
Search and meta queries: yes. But multiple meta queries: not necessarily. In your array-of-arrays, you can define the relationship. –  Chip Bennett Nov 7 '12 at 21:33
show 4 more comments

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.