按标签 Wordpress 元查询过滤器 [英] Meta Query Filter by Tag Wordpress
问题描述
我目前有多个页面,每个页面都有一个名为国家/地区"的自定义字段.
I currently have multiple pages, and in each page is a custom field which is named 'country'.
在国家/地区"字段中,我的值为英国、美国、巴西".
In the 'country' field I have this value 'uk, usa, brazil'.
我想要做的是在页面上显示带有我在自定义字段国家/地区"中列出的标签的帖子(在这种情况下,显示带有任何标签英国"、美国"和'巴西').
What I'm wanting to do is display posts on the page which have the tags I have listed in the custom field 'country' (In this case show posts which has any of the tags 'uk', 'usa' and 'brazil').
我有以下代码,但我不知道如何操作它来执行上述操作?
I have the following code, but I don't know how to manipulate it to do the above?
$args = array (
'post_type' => array( 'post' ), // YOUR POST TYPE
'meta_query' => array(
array(
'key' => 'country',
'value' => $your_country, // THE COUNTRY TO SEARCH
'compare' => 'LIKE', // TO SEARCH THIS COUNTRY IN YOUR COMMA SEPERATED STRING
'type' => 'CHAR',
),
),
);
// The Query
$query = new WP_Query( $args );
似乎只是过滤单个值?
对实现上述目标的任何帮助将不胜感激.
Any help to achieve the above would be greatly appreciated.
推荐答案
我会设置一个自定义分类法来创建另一个名为国家/地区"的标签"版本,然后您可以改为执行分类法查询.
I'd setup a custom taxonomy to create another version of 'tags' called 'country', then you can do a taxonomy query instead.
有关创建自定义分类法的简单界面,请参阅自定义帖子类型 UI".
See 'Custom Post Type UI' for an easy interface to create a custom taxonomy.
https://wordpress.org/plugins/custom-post-type-ui/
- 安装 CPT 用户界面(上面的插件).
- 转到菜单中的添加/编辑分类法".添加国家"作为分类法.将其附加到您的post"post_type 和page".
- 用您的国家/地区标签标记页面(用于显示帖子)和帖子".
然后您可以在页面模板中使用自定义 wp_query 来显示贴有国家/地区标签的帖子.
You can then use the custom wp_query in a page template, to show the posts, tagged with the country tag.
要获取与您的页面相关联的国家/地区条款的 ID:
To grab the ID's of the country terms, associated with your page:
$countryTerms = wp_get_post_terms($post->ID, 'country', array("fields" => "ids"));
$countryTerms = wp_get_post_terms($post->ID, 'country', array("fields" => "ids"));
然后更改您的查询参数以收集带有国家/地区标签的帖子.
Then change your Query arguments to gather the posts, tagged with the country.
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'country',
'field' => 'term_id',
'terms' => $countryTerms
),
),
);
如果您想坚持使用元字段,请尝试将您的TYPE"值更改为NUMERIC".
If you want to stick to a meta field, try changing your 'TYPE' value to 'NUMERIC'.
这篇关于按标签 Wordpress 元查询过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!