0

How to show sticky post of current browsing author on author.php template?

$sticky = get_option( 'sticky_posts' ); 
rsort( $sticky ); 
$sticky = array_slice( $sticky, 0, 5000 );
query_posts( array(
'post__in' => $sticky,
'author' => $current_user->ID,
'orderby' => 'rand',
'post_type' => 'post',
'post_status' => 'publish', 
'posts_per_page' => 5,
'caller_get_posts'=> 5 
) );

This works fine, but it shows sticky posts of all authors not just of current viewing author.

  • You should never use query_posts. It breaks the simple functionalities that you actually needed to make everything work. Also, note that caller_get_posts has been depreciated years ago. It should be ignore_sticky_posts. You should really turn debug on while developing as this is one notice that would have slapped your eyes first :-). Just one other thing, caller_get_posts should be either 0 or 1. 5 is invalid – Pieter Goosen Apr 19 '15 at 12:14
0

I find what was missing ($wp_query->queried_object_id;), here is answer:

$current_browsing_author = $wp_query->queried_object_id; // Get current browsing author
$sticky = get_option( 'sticky_posts' ); 
rsort( $sticky ); 
$sticky = array_slice( $sticky, 0, 5000 );
query_posts( array(
'post__in' => $sticky,
'author' => $author,
'orderby' => 'rand',
'post_type' => 'post',
'post_status' => 'publish', 
'posts_per_page' => 5,
'caller_get_posts'=> 5
) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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