因此,如果您只想在主页上显示登录用户感兴趣的帖子,可以使用the pre_get_posts-Action:
add_action( \'pre_get_posts\', \'wp123234_user_specific_loop\' );
function wp123234_user_specific_loop( $query ){
if( ! is_user_logged_in() ) //Just return, if the user is not logged in.
return;
if( ! $query->is_main_query() ) //If its not the main query return
return;
if( !$query->is_home() ) //If its not the "home"-query return
return;
$interests = get_user_meta( get_current_user_id(), \'interests\', true );
if( empty( $interests ) ) //If the user has no interests return
return;
/*Now, we want to display the specific posts.*/
//Assuming, the $interests-Array contains category-IDs
//$query->set( \'category__in\', $interests );
//Assuming, the $interests-Array contains custom fields of posts
$query->set( \'meta_query\', array( array(
\'key\' => \'interests\',
\'value\' => $interests,
\'compare\' => \'IN\',
) ) );
//Assuming, you want to ignore sticky posts, since they might show up, although the
//user is not interested in
$query->set( \'ignore_sticky_posts\', true );
}