我在我的网站上使用终极成员(member)插件。对于每个成员(作者),我有一个字段来存储成员性别。 (男士/女士)。
数据存储在我的数据库中,位于 wp_usermeta 内。
元键是“性别”,元值是“Homme”或“Femme”。
我正在尝试编写一个 wp-query 来显示所有作者的所有帖子,但仅显示“Homme”作者,而另一个仅显示“Femme”作者。
这是我的 wp-query,用于显示所有帖子而不按性别过滤:
<?php $custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
);
$custom_query = new WP_Query( $custom_query_args ); ?>
工作正常。
到目前为止,我已经尝试过只获取“Homme”性别的帖子,但它不起作用...我想我需要在某处添加对帖子作者 ID 的引用,但我找不到解决方案。
<?php $custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'gender',
'value' => 'Homme',
'compare' => '='
),
),
'order' => 'DESC',
'orderby' => 'date',
);
$custom_query = new WP_Query( $custom_query_args ); ?>
我不知道是否有办法用插件本身来完成它,但我很确定它可以通过一个简单的 wp-query 来完成。
有人可以帮我解决这个问题吗?
谢谢。
最佳答案
更改此:
<?php $custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'gender',
'value' => 'Homme',
'compare' => '='
),
),
'order' => 'DESC',
'orderby' => 'date',
);
为此:
<?php $custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'gender',
'meta_value' => 'Homme',
'order' => 'DESC',
'orderby' => 'date',
);
关于php - 按作者性别查询帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36774527/