So for my custom post I need to put the user id into the “author” parameter as shown below:
$args = array(
'paged'=>$paged,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'author' => ''
);
For example, I am in a “Ryan” profile page. The URL is as following:
http://example.com/?page_id=10&up_username=Ryan
If I am in “Steve” profile page, then the URL is as below:
http://example.com/?page_id=10&up_username=Steve
Each user has their own ID
.
For example, “Ryan” has ID of 35 while Steve has “ID=12”
If I put the ID number in the author
parameter, then I get the posts based on the ID number.
For example,
I put 'author' => '35'
, then I see Ryan’s post.
Now, how can I make it so that the "author"
parameter automatically picks up the ID number from the profile page?
I got some help (shown below) but it is not working.
// Default to false in case user can't be found.
$user_id = false;
// Clean username variable before use.
$username = ( ! empty( $_GET['up_username'] ) ) ? sanitize_text_field( $_GET['up_username'] ) : false;
// Check for user ID.
if ( $username && $user = get_user_by( 'slug', $username ) ) {
$user_id = $user->ID;
}
$args = array(
'paged'=>$paged,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'author' => $user_id
);
Any help will be much appreciated!
EDIT
From digging around, I found some info:
function id_to_member( $user_id ) {
$res = '';
$nice_url = userpro_get_option('permalink_type');
$user = get_userdata( $user_id );
if ($nice_url == 'ID') $res = $user_id;
if ($nice_url == 'username') $res = $user->user_login;
if ($nice_url == 'name') {
$res = $this->get_fullname_by_userid( $user_id );
}
if ($nice_url == 'display_name'){
$res = userpro_profile_data('display_name', $user_id);
}
if ($res != '')
return $res;
}
and
<a href="#" data-up_username="<?php echo $userpro->id_to_member($user_id); ?>" data-template="view" class="userpro-button secondary"><?php _e('View Profile','userpro') ?></a>
EDIT 2
So, I realized that the profile page was generated by plugin (API: http://userproplugin.com/userpro/docs/api.html)
I used the following and was able to get the user data:
<?php
global $userpro ;
$user_id = userpro_get_view_user( get_query_var('up_username') );
?>
<div class="user">
<?php echo userpro_profile_data( display_name, $user_id ); ?>
</div>
So, I am in “ryan’s page” and I was able to output “Ryan”, which means I am getting awfully close to “extract” the ID of the user.
Now, all I need to do is somehow find out the ID from this info.
Try using:
author_name
instead ofauthor
.Based on what I read here:
Give that a shot.
What if you tried using
'login'
rather than'slug'
in your get_user_by(…) call in your initial code?According to WP Codex, WP_Query accept
You can get user nice name by:
And then pass this user id in
$args
:Hope it helps.
Display Names don’t have to be unique, so this should search for all users with a given display name. Then join them with a comma before preforming your paginated search: