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 writing a loop for multi post types with meta_key filters. The filters are inputs from visitors. When visitor click a link(filter), The form submitted and I get the filter. Now, should I just use $query->set() to change the query args, or should I submit new query? Currently I'm using this function to get arguments from input and pass the arguments to wp_query:

function get_args($args){
$defaults= array( 
   // some standard query args
   )
$args = wp_parse_args ( $args, $defaults );
extract ( $args );
if(!empty($args['filter'])){
   switch ($args['type']){
      case 'top_voted':
         $args['post_type'] = 'docs';
         $args['meta_key'] = '_vote_total';
         $args['order'] = 'DESC';
         $args['orderby']='meta_value_num';
       break;
      case 'unanswered_questions':
         $args['post_type] = 'topics';
         $args['blabla'] = 'blabla;
      break;
      default:
          blabla;
       break;
}
return $args;
}

In the template, I use this:

$key = isset( $_POST['filter'] ) ? array_keys($_POST['filter']) : null;
$query_args = get_args(array('user_id'=>$user_id, 'filter'=$key));
$r = new WP_Query( $query_args);

In the above example, I think I'm using new query each time the filter changes. If query->set() or other filters can improve the performance, please help me understand how to do it. Thanks!

share|improve this question
1  
If you can alter a query before it runs, then do so and save the database some work. You might be able to alter the query with query->set() and if not you can use the posts_where, posts_fields, etc filters. It is hard to say whether any of that is best for you without more information. –  s_ha_dum Oct 17 '12 at 0:22
    
@s_ha_dum Post this as answer, please. –  kaiser Oct 17 '12 at 2:41
    
@s_ha_dum I just updated my question, would you please help evaluating the code and suggest how to improve it? –  Jenny Oct 17 '12 at 3:09
    
Posted my comment as an answer as requested. –  s_ha_dum Oct 17 '12 at 4:45
add comment

1 Answer

up vote 2 down vote accepted

If you can alter a query before it runs, then do so and save the database some work. You might be able to alter the query with query->set() and if not you can use the posts_where, posts_fields, etc filters. It is hard to say whether any of that is best for you without more information.

Edit:

Another question, what kind of 'page' is this loading on? Here is my guess: you created a 'Page' from wp_admin->Pages, and probably assigned a custom built page template. If that is the case you probably can't mess with the main query as its job is going to be to load the 'Page' you created, which means you seem to be on the right track with creating the new query.

share|improve this answer
    
Initially I did created a page. But I failed to make the pagination links working. So, I changed to make it a widget and load the widget at front page, --this allows me to use javascript to create pagination links. –  Jenny Oct 17 '12 at 5:14
    
Then you are doing it about the only way you can. You can't manipulate the main query under that circumstance. –  s_ha_dum Oct 17 '12 at 5:29
    
Thanks for helping! –  Jenny Oct 17 '12 at 5:31
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.