Filter archive.php by custom meta

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?

Read More

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?

Related posts

2 comments

  1. 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 );
    
  2. 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 );
    

Comments are closed.