Query posts only if meta_value is not empty

Can't figure out why this isn't working. I'm trying to query posts for a member mailing list and simply d

Can't figure out why this isn't working. I'm trying to query posts for a member mailing list and simply don't want to include users that are not members. Right now even the users that have an empty meta_value to the meta_key 'member' are shown. Please help! (WP version 5.3.2)

    function get_all_users () {


        $args = array( 
            'orderby' => 'display_name',
            'meta_query' => array (
    array (

            'key' => 'member',
            'value' => '',
            'compare' => '!='
        )
      )
    );

$users = get_users($args);

        ob_start();

// Array of WP_User objects.
        foreach ( $users as $user ) {

            $firstName = get_user_meta($user->ID, 'first_name', true);
            $lastName = get_user_meta($user->ID, 'last_name', true);
            echo "<li><a href=\"mailto:" . $user->user_email . "\">" . $firstName . " " . $lastName . "</a></li>";
        }
        return ob_get_clean();
    }

Can't figure out why this isn't working. I'm trying to query posts for a member mailing list and simply don't want to include users that are not members. Right now even the users that have an empty meta_value to the meta_key 'member' are shown. Please help! (WP version 5.3.2)

    function get_all_users () {


        $args = array( 
            'orderby' => 'display_name',
            'meta_query' => array (
    array (

            'key' => 'member',
            'value' => '',
            'compare' => '!='
        )
      )
    );

$users = get_users($args);

        ob_start();

// Array of WP_User objects.
        foreach ( $users as $user ) {

            $firstName = get_user_meta($user->ID, 'first_name', true);
            $lastName = get_user_meta($user->ID, 'last_name', true);
            echo "<li><a href=\"mailto:" . $user->user_email . "\">" . $firstName . " " . $lastName . "</a></li>";
        }
        return ob_get_clean();
    }
Share Improve this question edited Feb 21, 2020 at 14:43 lindag. asked Feb 21, 2020 at 12:59 lindag.lindag. 311 silver badge5 bronze badges 1
  • 1 You define get_all_users but call get_users. – Vitauts Stočka Commented Feb 21, 2020 at 16:23
Add a comment  | 

2 Answers 2

Reset to default 2

For some reason this worked. Notice that there is a space in the meta_value.

$args = array( 
        'orderby' => 'display_name',
        'meta_key' => 'member',
        'meta_value' => ' ',
        'meta_compare' => '!='
    );

Try removing they value from your meta query:

$args = array(
    'orderby'        => 'display_name',
    'meta_query'     => array(
        array(
            'key'     => 'member',
            'compare' => '!=',
        ),
    ),
);

$users = get_users($args);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744720201a4589863.html

相关推荐

  • Query posts only if meta_value is not empty

    Can't figure out why this isn't working. I'm trying to query posts for a member mailing list and simply d

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信