0

I need to print the user's id into this array but it doesn't seem to be working. I have tried all sorts of things from $user->ID to $user_id to no avail. I am trying to get the value from my functions.php Any help would be greatly appreciated!

Here what's in my php function:

$user_page = get_queried_object();
$user_id = $user_page->data->ID;
$args = array(
        'post_type' => 'listings',
        'post_status'    => 'publish',
        'meta_query' => array(
            array(
                'key' => 'stm_car_user',
                'value' => $user_id
            )
        )
    );

$query = new WP_Query($args);

I'm sorry if my question is a little vague, here is what I am trying to achieve I don't want to get the id of the currently logged in user, I want to get the id of the user that the information is on, I am displaying the user's posts on their profiles and I need their id's displayed dynamically for each profile, you get me? I hope this clarifies things...

10
  • I approached by get_users method. It returns user object, and it has ID property. also wp_get_current_user methods can help as well.
    – idpokute
    Commented May 18, 2018 at 13:52
  • @idpokute I'm sorry, I'm kind of a newbie, how would you rewrite the array for the way that you're saying that it works?
    – Matt H
    Commented May 18, 2018 at 13:56
  • Note that using meta_query is terribly slow/expensive, you should be better off using a taxonomy named stm_car_user where the term names are user IDs, it would be significantly faster
    – Tom J Nowell
    Commented May 18, 2018 at 14:17
  • @TomJNowell How would I go about writing that please?
    – Matt H
    Commented May 18, 2018 at 14:27
  • @MattH That would be a good question, but not something for the comments, having said that any answer would basically just be a more verbose version of my comment
    – Tom J Nowell
    Commented May 18, 2018 at 14:30

4 Answers 4

1

The answer to your question is literally the title with underscores instead of spaces:

$user_id = get_current_user_id();

https://developer.wordpress.org/reference/functions/get_current_user_id/

2
  • I don't want to get the id of the currently logged in user, I want to get the id of the user that the information is on, I am displaying the user's posts on their profiles and I need their id's displayed dynamically for each profile, you get me?
    – Matt H
    Commented May 18, 2018 at 14:25
  • Ah, that's not what your question asks, can you edit your question to remove that ambiguity? You never qualified current, I'd recommend instead saying the author of a post
    – Tom J Nowell
    Commented May 18, 2018 at 14:32
0

If I'm not wrong it should be

$user_page->ID

Also you can check

get_queried_object_id()
1
  • Unfortunately still doesn't work....
    – Matt H
    Commented May 18, 2018 at 13:59
0

Here is the example, this is display all users in subscribers role.

$subscribers = get_users( [ 'role__in' => [ 'subscriber' ] ] );
// Array of WP_User objects.
foreach ( $subscribers as $user ) {
    echo '<span>' . $user->ID . '</span>';
}
5
  • How do I echo the $user->ID outside of the loop though? It doesn't work..
    – Matt H
    Commented May 18, 2018 at 14:26
  • You get the all the subscribers, which is array. You can access the first user id in the array using array syntax. For instance, $subscribers[0]->ID;
    – idpokute
    Commented May 18, 2018 at 15:15
  • Didn't work unfortunately....
    – Matt H
    Commented May 18, 2018 at 15:30
  • @MattH I saw the duplicated post of yours which is about ajax request and response. If I understand correctly, you are trying to make infinite scroll. If you don't have any subscriber, it won't work. Change subscriber to administrator, check whether it is working or not using var_dump. By the way what role of users do you want to pull?
    – idpokute
    Commented May 18, 2018 at 15:39
  • I am not trying to make an infinite scroll but if you have a solution for the infinite scroll then I would greatly appreciate it and I want to get the id of all users by all roles...
    – Matt H
    Commented May 18, 2018 at 15:47
0

If you use an author.php file in your theme then it would automatically get the posts from that author.

https://codex.wordpress.org/Author_Templates#Which_Template_File_is_Used.3F

That way you don't need to manually code your own and wp core code to link to author profiles would work without any modifications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.