最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Normal PHP array for exclude section of WordPress query?

matteradmin6PV0评论

I just wanted to check on something with the community here and avoid any future drama.

I have a variable which seems to be working fine in a wp_user_query but not sure if I'm missing something that will trip me later on.

I have the following in on a page template:

<?php

// WP_User_Query arguments
$args = array(
    'role'           => 'editor',
    'exclude'        => $userID,
    'number'         => '999999',
    'count_total'    => false,
);

// The User Query
$user_query = new WP_User_Query( $args );

// The User Loop
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        // do something

         echo $user->display_name;

    }
} else {
    // no users found
}


?>

WordPress is expecting is an array in the exclude section like this:

Array ( 1 , 2 , 3 , 4 )

However When I use

<?php print_r($userID) ?>

I get an array that looks like this

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

Do I need to strip the key and replace it with a comma?

I just wanted to check on something with the community here and avoid any future drama.

I have a variable which seems to be working fine in a wp_user_query but not sure if I'm missing something that will trip me later on.

I have the following in on a page template:

<?php

// WP_User_Query arguments
$args = array(
    'role'           => 'editor',
    'exclude'        => $userID,
    'number'         => '999999',
    'count_total'    => false,
);

// The User Query
$user_query = new WP_User_Query( $args );

// The User Loop
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        // do something

         echo $user->display_name;

    }
} else {
    // no users found
}


?>

WordPress is expecting is an array in the exclude section like this:

Array ( 1 , 2 , 3 , 4 )

However When I use

<?php print_r($userID) ?>

I get an array that looks like this

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

Do I need to strip the key and replace it with a comma?

Share Improve this question edited Apr 6, 2019 at 16:46 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Apr 5, 2019 at 16:34 NeilNeil 344 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You're just seeing the indices (indexes).

<?php

$array = array( 1,2,3,4 );
print_r( $array);

/*
outputs: 
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
*/
Post a comment

comment list (0)

  1. No comments so far