• I try to get custom posts with predefined meta value.The query return nothing.

        $args = array
        (
            'post_type' => 'item',
            'nopaging' => true,
            'tax_query' => array
            (   array
                (
                'taxonomy' => $taxonomy,
                'field' => 'term_taxonomy_id',
                'terms' => $term_id
                )
            ),
            'meta_query'  => array(
                array(         
                    'key'     => 'from_import',  
                    'value'   => '1', 
                )
            )
        );
        $posts = query_posts( $args );

    When I remove meta_query or tax_query it works fine. How I can combine it?

    • This topic was modified 7 years, 7 months ago by roissya24.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Do you need posts with a taxonomy term OR a meta value of 1? Or must both conditions be met? WP always assumes you want both to be true. In order to use OR logic, the query needs to be filtered, or just run 2 queries, one for each condition.

    If both need to be met, your args array appears to be correct. You’re sure such item posts with both conditions exist? Perhaps examining the resulting SQL query may give you a clue to why nothing is returned. The SQL should be in the global $wp_query->request property.

    FWIW, you shouldn’t be using query_posts(). Either alter the main query through the pre_get_posts action or use get_posts() or create a new WP_Query.

    Thread Starter roissya24

    (@roissya24)

    Hi, thank you for reply. Yes, I need both of that conditions. I check posts custom meta (correct) and taxonomy. But when I try to run query I see white screen (error debug is on).

    Moderator bcworkz

    (@bcworkz)

    Try using ‘term_id’ instead of ‘term_taxonomy_id’. It’s quite unusual to use ‘term_taxonomy_id’, it’s more of an internal value than what anyone normally uses.

    Denis B

    (@fortisthemes)

    @roissya24 did you ever get a solution to this? I’m having the same issue, meta_query & tax_query work alone, when combined not so well.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Combine tax_query and meta_query in query_posts()’ is closed to new replies.