自定义字段查询多个帖子类型

编程入门 行业动态 更新时间:2024-10-27 17:12:21
自定义字段查询多个帖子类型 - 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.

更多推荐

本文发布于:2023-07-31 01:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340422.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   自定义   字段   类型   帖子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!