-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom query for Author results in a WordPress database error #625
Comments
A previous change broke the regEx which then broke the SQL which then broke the front end.
@hyperionjrw Do you have any steps on how to reproduce the issue? |
With a clean WordPress install do the following:
You need to make sure that the user ID has a zero in it and it doesn't have a zero in the last two chars otherwise the problem won't surface. This is all down to the fact that the new $id=0; line produces valid regex that's looking for |
Thanks for providing detailed steps, @hyperionjrw! Much appreciated -- I've been able to reproduce now. I've taken a look at the PR and I'm happy to merge once the changes have been completed. Thanks again! |
Added some comments and fixed a wonky commit.
Changing the order to match previous commit.
Closing per #628. |
A previous change broke the regEx which then broke the SQL which then broke the front end.
Added some comments and fixed a wonky commit.
Changing the order to match previous commit.
With a custom query for an Author's posts WordPress will throw a DB error due to malformed SQL.
Before your post_where filter runs the query looks like this:
' AND wp_posts.post_author IN (10202675) AND wp_posts.post_type = \'post\' AND ((wp_posts.post_status = \'publish\'))'
After it looks like this:
' AND ( wp_posts.post_author IN (10202 OR (wp_term_taxonomy.taxonomy = \'author\' AND wp_term_taxonomy.term_id = \'48333\'))675) AND wp_posts.post_type = \'post\' AND ((wp_posts.post_status = \'publish\'))'
You'll notice that the user ID goes from 10202675 to 10202--SQL--675
The problem is down to a bug in the posts_where_filter method when setting $id.
If it's not an author page it is set to '\d+' to look for digits when passed to the regex but before then that $id is passed to get_user_by which will fail and then the value is set to 0. That 0 is then passed into the regex and ends up splitting longer user_ids at one position after the last zero.
regex:
post_author\s*IN\s*(.*0(?:.*private\')?.)
So:
$id = is_author() ? get_queried_object_id() : '\d+';
if ( false === get_user_by( 'id', $id ) ){
$id = 0;
}
Needs to become:
$id = is_author() ? get_queried_object_id() : '\d+';
if ( false === get_user_by( 'id', $id ) ){
$id = '\d+';
}
Although I think there will still be a problem if you try to query a different author's posts on another author's page as the id will always come from the page rather than the custom query.
The text was updated successfully, but these errors were encountered: