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 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
    

2 Answers 2

up vote 2 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.