自定义字段查询多个帖子类型 - Wordpress(Custom field query multiple post types - Wordpress)
我试图查询两个自定义帖子类型的数据,以下是不起作用。 我尝试了几个版本......但没有运气。
$args = array( 'post_type' => array('custom1', 'custom2'), 'meta_query' => array( 'relation' => 'AND', //the first two keys are custom fields found in custom1 custom posts array( 'key' => 'firstKey', 'value' => 3 ), array( 'key' => 'secondKey', 'value' => cool ), //the third key can be found in custom2 custom posts array( 'key' => 'thirdKey', 'value' => 3 ) ) );I m trying to query two custom post types for data and the below isn't working. I tried a few versions ... but no luck.
$args = array( 'post_type' => array('custom1', 'custom2'), 'meta_query' => array( 'relation' => 'AND', //the first two keys are custom fields found in custom1 custom posts array( 'key' => 'firstKey', 'value' => 3 ), array( 'key' => 'secondKey', 'value' => cool ), //the third key can be found in custom2 custom posts array( 'key' => 'thirdKey', 'value' => 3 ) ) );最满意答案
根据您的意见:如果要对不同类型的用户使用自定义查询,则可能需要动态创建查询。
例如,如果“酷”类型的用户获得特殊待遇,那么基本想法将是:
$args = array( 'post_type' => 'custom' ); if (user is 'cool') { $args['meta_query'] = array( 'relation' => 'AND', array( 'key' => 'secondKey', 'value' => 'cool' ), array( 'key' => 'thirdKey', 'value' => 3 ) }; } else { $args['meta-key'] = 'thirdKey'; $args['meta_value'] = 3 }这只是像WP_Query($ args)那样传递它,如果它是静态的那样。
Based on your comments: If you want to use custom queries for different types of users, then you probably want to create your query dynamically.
For example, if users of type 'cool' get special treatment, then the basic idea would be:
$args = array( 'post_type' => 'custom' ); if (user is 'cool') { $args['meta_query'] = array( 'relation' => 'AND', array( 'key' => 'secondKey', 'value' => 'cool' ), array( 'key' => 'thirdKey', 'value' => 3 ) }; } else { $args['meta-key'] = 'thirdKey'; $args['meta_value'] = 3 }This just pass this as WP_Query($args) like you would if it had been static.
更多推荐
发布评论