【问题标题】:query post by author gender按作者性别查询帖子
【发布时间】:2016-04-22 00:21:48
【问题描述】:

我在我的网站上使用了 utimate 会员插件。对于每个成员(作者),我都有一个字段来存储成员性别。 (男人/女人)。

数据存储在我的数据库中,在 wp_usermeta 中。

元键是“性别”,元值是“Homme”或“Femme”。

我正在尝试编写一个 wp 查询来显示所有作者的所有帖子,但只有“Homme”作者,另一个只有“Femme”。

这是我的 wp 查询,用于显示所有帖子而不按性别过滤:

<?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 mysql wordpress meta-key


    【解决方案1】:

    改变这个:

    <?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',
    );

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 2023-01-20
      • 2021-06-07
      • 1970-01-01
      相关资源
      最近更新 更多