簡體   English   中英

在query_posts()中合並tax_query和meta_query

[英]Combine tax_query and meta_query in query_posts()

我嘗試獲取具有預定義元值的自定義帖子。查詢不返回任何內容。

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

當我刪除meta_query或tax_query時,它工作正常。 我該如何結合呢?

我相信這應該可以解決您的代碼。 我實際上還沒有運行它,但可以嘗試一下。 您每個人都有一個額外的“ array()”。

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

我只是介紹了這一點,這是我正在使用的片段,請根據需要進行調整以滿足您的需求。

// Bring post from the global context (if not present already).
global $post;

// Define the post_type's to query for.
$post_types = array( 'event', 'post', 'book' );

// Do the weird query. 
// Play with, or add arguments as needed https://codex.wordpress.org/Class_Reference/WP_Query
$results = WP_Query(
        array(
            'post_type' => $post_types,
            'tax_query' => array(
                array(
                    'taxonomy' => 'category',
                    'terms' => wp_get_post_categories( $post->ID )
                )
            ),
            'meta_query' => array(
                'relation' => 'OR',
                array(
                    'key'     => 'presenters_people',
                    'value'   => $post->ID,
                    'compare' => 'LIKE'
                ),
                array(
                    'key'     => 'author',
                    'value'   => $post->ID,
                    'compare' => 'LIKE'
                )
            )
        )
    );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM