3

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?

1

2 Answers 2

7

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 );
1
  • 1
    Please add an explanation to your answer: why could that solve the problem?
    – fuxia
    Sep 4, 2013 at 22:52
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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