WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I"m trying to filter an archive page by custom meta query.

I've tried using query_posts($args) prior to the loop, but it returns nothing. Any ideas?

Here's what I have so far:

<?php 
        //$wolfName = $_GET['wolfName'];
        $archiveArgs = array(
                        'meta_key'  => 'wolf',
                        'meta_value'    => 'boltz'
                        );
        query_posts($archiveArgs);
        ?>
        <?php if (have_posts()) : ?>

It returns no posts... any ideas?

share|improve this question
    
up vote 3 down vote accepted

Use pre_get_posts action hook

function archive_meta_query( $query ) {
    if ( $query->is_archive){
      $query->query_vars["meta_key"] = 'wolf';
      $query->query_vars["meta_value"] = 'boltz';
    }
}
add_action( 'pre_get_posts', 'archive_meta_query', 1 );
share|improve this answer
1  
Please add an explanation to your answer: why could that solve the problem? – toscho Sep 4 '13 at 22:52

thanks for ur code Strik3r ..this helped me a lot..I finally got sorting by custom field to work...in functions.php

function sort_meta_query( $query ) {
 if ( $query->is_archive || $query->is_category || $query->is_home ){
  $query->query_vars["meta_key"] = 'fastest_speed_experienced';
  $query->query_vars["orderby"] = 'meta_value_num';
 }
}
  add_action( 'pre_get_posts', 'sort_meta_query', 1 );
share|improve this answer

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.