[wp-trac] [WordPress Trac] #40042: Empty Author ID on first time action pre_get_post is called.
WordPress Trac
noreply at wordpress.org
Mon Mar 6 11:14:41 UTC 2017
#40042: Empty Author ID on first time action pre_get_post is called.
--------------------------+------------------------------
Reporter: BackuPs | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version: 4.7.2
Severity: normal | Resolution:
Keywords: | Focuses:
--------------------------+------------------------------
Comment (by BackuPs):
Note: I am talking about the author page listing all his posts he created.
The only way to get to the user meta is by the user id. But that id is
missing from the query.
The code is pretty simple but does not work as the author field has no
info about the id and thus cant work.
{{{#!php
<?php
function set_user_pagination( $query ) {
if (is_admin()) return;
if (is_author() && $query->is_main_query()) {
$author_id = get_query_var('author');
if (!empty($author_id)) {
$pagination=get_the_author_meta('pagination',$author_id);
//$pagination= get_user_meta($author_id,
'pagination', true);
if (!empty($pagination)) {
$query->set( 'posts_per_page', $pagination
);
}
}
}
}
add_action( 'pre_get_posts', 'set_user_pagination' );
}}}
Now i know i can code around this issue. But that is very cumbersome. And
does it work in a multisite?
The question is why in the first time the action is called the $query does
not have the user id set?
{{{
function set_user_pagination( $query ) {
if (is_admin()) return;
if (is_author() && $query->is_main_query()) {
$author_id = get_query_var('author');
if (empty($author_id))
$author_id=get_query_var('author_name');
if (!is_numeric($author_id)) {
$users = get_users( array( 'fields' => array( 'ID'
) ) );
foreach($users as $user_id){
$nickname = get_user_meta (
$user_id->ID,'nickname', true);
if ($author_id==$nickname) {
$author_id= $user_id->ID;
break;
}
}
}
if (!empty($author_id)) {
$pagination= get_user_meta($author_id,
'pagination', true);
if (!empty($pagination)) {
$query->set( 'posts_per_page', $pagination
);
}
}
}
}
add_action( 'pre_get_posts', 'set_user_pagination' );
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/40042#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list