Wordpress query with sub relations

后端 未结 1 1947
夕颜
夕颜 2021-01-27 12:32

Is there a way to have a meta_query with sub relations? I don\'t want one relation to apply to all of the query\'s arrays.

I tried this, but to

相关标签:
1条回答
  • 2021-01-27 12:57

    It looks like this could be a meta_query limitation. WP Codex shows that you can use relations for meta queries, but as far as I see the limitation is that you can only use one kind of relation for the whole meta_query.

    This means you can have a full OR or a full AND (default) relation between meta_query parameters, but you can never have them both combined, which is just what you want.

    As I was not glad when I faced it at WP Codex, I went deeper in the matter by manually searching the code. So I found out that WP_Query calls WP_Meta_Query to parse meta_query parameters. This is WP_Meta_Query's class constructor (you can find it in wp-includes/meta.php at line 643 in WP 3.6):

    /**
     * Constructor
     *
     * @param array $meta_query (optional) A meta query
     */
    function __construct( $meta_query = false ) {
        if ( !$meta_query )
            return;
    
        if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {
            $this->relation = 'OR';
        } else {
            $this->relation = 'AND';
        }
    
        $this->queries = array();
    
        foreach ( $meta_query as $key => $query ) {
            if ( ! is_array( $query ) )
                continue;
    
            $this->queries[] = $query;
        }
    }
    

    Long story short, the second if shows that this class will work only either with a OR or AND relation, but never with both of them combined.

    So, unfortunately, it seems that it's not possible to achieve what you are trying to do (at least until WP 3.6). I guess your best shot is to split the main query, first doing specific Meta Queries and then applying their results(returned metas) to your main query.

    I know that this is not a solution but an answer. Nevertheless, I hope this may help you to find a solution for this matter.

    0 讨论(0)
提交回复
热议问题