- This topic has 9 replies, 2 voices, and was last updated 4 years, 10 months ago by Tom.
-
AuthorPosts
-
May 21, 2019 at 6:10 am #906558culpable
Hiya,
I noticed that on the staging version of my website there are no longer author archives. They are currently showing “Nothing Found”.
I was given the following code a couple of months ago to get pages (as in, wordpress “pages”) to show in the author archives:
add_action( 'pre_get_posts', function( $query ) { if ( ! $query->is_main_query() || is_admin() ) { return; } if ( $query->is_author ) { $query->set( 'post_type', array( 'post', 'page' ) ); $query->set( 'post__not_in', array(320,25753,25821,25823,25824,25825,25830,25832,25390,25963,26018,26002,25998,25977,26006,26004,25614,25984,25986,25994,25990,26000,25982,25975,26004) ); } } );
This was working perfectly for a long time, and still is on the live site.
These are the differences I can see between the two sites:
Live site (Author archives working):
GP Premium Version 1.7.8Staging site (Author archives not working):
GP Premium Version 1.8.2In addition, two related questions in terms of the efficiency above SQL query:
1. The majority of the excluded post IDs above are for WP Show Posts. Is it possible to run this query in a more efficient manner (exclude all WP Show Posts? Or maybe “tag” the WP Show Posts somehow to group them)?
2. The current PHP code above is set to “Run everywhere” on the site. Would it be more efficient to convert it into a shortcode, and then execute that shortcode on only the author archive pages via GP Elements (since these are the only relevant place this query needs to run)?Thank you in advance for your assistance.
May 21, 2019 at 5:10 pm #907256TomLead DeveloperLead DeveloperHi there,
1. Do they re-appear if you deactivate that PHP?
2. What do you mean by WP Show Post? Like those IDs are for WPSP lists themselves? If so, they shouldn’t display by default as they aren’t posts or pages.
3. I doubt it would make a difference performance-wise.
Let me know 🙂
May 22, 2019 at 5:17 am #907689culpableHi Tom,
1. No, they don’t reappear. I triple checked and it’s running the same code as the live site. This is quite the show stopper :\
2. My bad – I made a mistake. Those are navigational pages which contain post lists from wp show posts (forget I mentioned it)
3. If you say so 🙂
I’m on WordPress 5.1.1 (on both live and staging)
Please send help :c
May 22, 2019 at 2:53 pm #908256TomLead DeveloperLead DeveloperCan you try re-saving your permalinks in “Settings > Permalinks”?
If that doesn’t work, can you try deactivating your plugins one by one to check for conflicts?
May 22, 2019 at 3:26 pm #908280culpableNeither saving permalinks nor deactivating plugins seemed to fix the issue.
But… I did make a useful discovery.
As mentioned there are no posts on the site, only pages. I added a post and that post appeared (by itself) under its author name.
So it seems like the issue relates to correctly executing the query that relates pages to an author.
In both the live and staging site, I’m using woody snippets (https://wordpress.org/plugins/insert-php/) to insert the PHP (note they recently had a name change). I’ve tripled checked and in both cases the query is the same (as in OP).
May 23, 2019 at 6:55 am #908827TomLead DeveloperLead DeveloperCan you try this?:
add_action( 'pre_get_posts', function( $query ) { if ( ! $query->is_main_query() || is_admin() ) { return; } if ( $query->is_author() ) { $query->set( 'post_type', array( 'post', 'page' ) ); $query->set( 'post__not_in', array(320,25753,25821,25823,25824,25825,25830,25832,25390,25963,26018,26002,25998,25977,26006,26004,25614,25984,25986,25994,25990,26000,25982,25975,26004) ); } } );
May 23, 2019 at 7:01 am #908842culpableI implemented it with optimism. Unfortunately, it’s not working (cache cleared)
May 23, 2019 at 7:09 am #908852TomLead DeveloperLead DeveloperWhat if you remove this temporarily?:
$query->set( 'post__not_in', array(320,25753,25821,25823,25824,25825,25830,25832,25390,25963,26018,26002,25998,25977,26006,26004,25614,25984,25986,25994,25990,26000,25982,25975,26004) );
May 23, 2019 at 7:18 am #908862culpableHaha – funnily enough that’s the first thing I tried next after confirming that your suggestion wasn’t working. Unfortunately, this didn’t help either.
But good news Tom – I think I found the issue.
For whatever reason, the version differences in the plugin I’m using to insert PHP – Woody Ad Snippets (https://wordpress.org/plugins/insert-php/) – is causing this to fail.
I downloaded the code snippets plugin you recommended (https://en-ca.wordpress.org/plugins/code-snippets/) and added the same code.
Both the new and old version of the code you suggested worked, and the pages are displaying. Very strange.
This is a peculiar error as the other PHP snippets in the plugin are working correctly, AND the same code is working on the live site. I have no idea what could have possibly changed between versions for this odd error to occur. Regardless, this is the nudge I need to move my stuff over into code snippets… that’s what I get for not taking your recommendations seriously 😉
No, but seriously – I apologise for wasting your time. Thank you very much for helping me to navigate this tricky issue.
One last thing – which of the above queries (the original one I posted, or the new one you posted) is preferred? (I realize they are quite similar, but still – is one of the two a ‘best practice’)?
Thank you for your time Tom 🙂
May 23, 2019 at 7:32 am #908873TomLead DeveloperLead DeveloperAh – glad you found the issue! Code Snippets is a great plugin.
If your original code works, it should be fine. I was just trying small tweaks to see if we could find any hints at what was going on 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.