2

I want fetch all posts from the post type book in the custom taxonomy book_tags with the meta key lang and the meta value en.

My code:

<?php 
$args = array( 
    'post_type'      => 'book',
    'posts_per_page' => 40,
    'paged'          => "$paged", 

    'meta_query'     => array(
        array( 
            'key'     => 'lang',
            'value'   => 'en',
            'compare' => 'LIKE'
        )
    ),
    'tax_query' => array(
        array(
            'taxonomy' => 'book_tags',
            'field'    => 'slug',
            'terms'    => get_queried_object()->slug
        )
    )
);

$additional_loop = new WP_Query($args); 

while ($additional_loop->have_posts()) : 
    $additional_loop->the_post();

This doesn’t work? Why?

If I remove "meta_query" it works. Is there a bug in "meta_query"?

  • 1
    Please explain precisely what you are trying to do and what isn't working (compared to what you are expecting)... because it is possible there is a better way to do what you want. For example filtering pre_get_posts is a very common solution that people overlook when trying to modify a loop. Though for a start, I'd guess that "$paged" is wrong (since the variable is in quotes) as might also be get_queried_object()->slug. – helgatheviking Jul 22 '13 at 3:48
  • if I remove "meta_query" then It'work, because I think "meta_query" have bug. I want display all custom post "lang=en". Sorry my bad EL – kytdesigner Jul 22 '13 at 3:52
  • Your English is fine, but you aren't saying much for me to go on. :) You want to show all custom posts with lang=en for a specific term. Is this on a custom taxonomy archive? Custom post archive? There is no bug with meta_query. Please edit your question to provide more details about what you are trying to do.. and where. – helgatheviking Jul 22 '13 at 14:00
  • First, Thank you so much. I want to show all custom posts with lang=en on a custom taxonomy archive. Plz help me, and what's your email address? Nice to meet you, friend. – kytdesigner Jul 23 '13 at 14:43
1

Since in your comment, you said that you just want posts with meta key lang=en on a custom taxonomy page, the easiest way to do that would be to filter the query with pre_get_posts before it is run.

function wpa_107371_meta_query( $query ) {
    if ( is_admin() || ! $query->is_main_query() )
        return;

    // only change the query on a custom taxonomy
    // can check for a specific taxonomy if desired
    if ( is_tax() ) {
        //define our meta query
              $meta_query = array(
                    'key'=>'lang',
                    'value'=>'en',
                    'compare'=>'=',
                );
         $query->set('meta_query',$meta_query);
        return;
    }

}
add_action( 'pre_get_posts', 'wpa_107371_meta_query' );
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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