Emily Turcato
Emily Turcato

Reputation: 591

Custom post with author

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.

Upvotes: 1

Views: 135

Answers (5)

Ultimater
Ultimater

Reputation: 4738

Based on what I read here:

global $userpro;

$args = array(         
    'paged'=>$paged,
    'posts_per_page' => 10,         
    'orderby' => 'date',            
    'order' => 'DESC',
    'author' => $userpro->get_member_by_queryvar_from_id()
);

Give that a shot.

Upvotes: 1

Ultimater
Ultimater

Reputation: 4738

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:

$usersMatchedWithDisplayName=array();
if(!empty($_GET['up_username']))
{
        $user_query = new WP_User_Query(array(
          'search' => $_GET['up_username'],
          'search_fields' => array('display_name')
        ));
    if ( ! empty( $user_query->results ) )
    {
        foreach ( $user_query->results as $user )
        {
            $usersMatchedWithDisplayName[]=$user->ID;
        }
    }
}

$authors=implode(',',$usersMatchedWithDisplayName);

$args = array(         
    'paged'=>$paged,
    'posts_per_page' => 10,         
    'orderby' => 'date',            
    'order' => 'DESC',
    'author' => $authors
);

Upvotes: 1

Touqeer Shafi
Touqeer Shafi

Reputation: 5264

According to WP Codex, WP_Query accept

  • author (int) - use author id.
  • author_name (string) - use 'user_nicename' - NOT name.

You can get user nice name by:

$user = get_user_by( 'id', 35 ); // Ryan ID is 35, TODO: you can get this from query string

And then pass this user id in $args:

$args = array(         
    'paged'=>$paged,
    'posts_per_page' => 10,         
    'orderby' => 'date',            
    'order' => 'DESC',
    'author_name' => $user->user_nicename
);

Hope it helps.

Upvotes: 1

Ultimater
Ultimater

Reputation: 4738

What if you tried using 'login' rather than 'slug' in your get_user_by(...) call in your initial code?

Upvotes: 1

Ultimater
Ultimater

Reputation: 4738

Try using:

author_name instead of author.

$args = array(         
    'paged'=>$paged,
    'posts_per_page' => 10,         
    'orderby' => 'date',            
    'order' => 'DESC',
    'author_name' => empty($_GET['up_username'])?false:$_GET['up_username']
);

Upvotes: 2

Related Questions