- This topic has 26 replies, 4 voices, and was last updated 4 years, 1 month ago by
David.
-
AuthorPosts
-
August 1, 2021 at 9:52 am #1879606
Malin
Hi,
Thank you for your patience in answering questions to help me set up.
I have the dispatch theme here:
https://www.alternativephotography.com/
The list “Header” I have selected all categories (around 40) apart from Events (15 categories) (they are listed in the yellow sidebar) and Suppliers directory (8 categories) (they don’t need to show on homepage).
Now the issue is that the first 4 posts seem to adhere to this selection but the posts below show everything, and also repeats the first four posts. How do I fix this?
Appreciate your help!August 1, 2021 at 11:32 pm #1880055Elvin
StaffCustomer SupportHi there,
Can you try this workaround PHP snippet?
add_filter('post_link', 'track_displayed_posts'); add_action('pre_get_posts','remove_already_displayed_posts'); $displayed_posts = []; function track_displayed_posts($url) { global $displayed_posts; $displayed_posts[] = get_the_ID(); return $url; // don't mess with the url } function remove_already_displayed_posts($query) { global $displayed_posts; $query->set('post__not_in', $displayed_posts); }
Here’s how to add PHP snippets – https://docs.generatepress.com/article/adding-php/
Let us know how it goes. ๐
August 2, 2021 at 6:35 am #1880526Malin
Hi Elvin!
Thank you!
It did something, but it didn’t work. The four top ones are:
2nd oldest post
3rd oldest
4th oldest
5 oldest
The list below is:
All posts, newest first and then all, including the 4 on top.
Should I have added a priority on the snippet or done something else?
https://www.alternativephotography.com/
Thank you for your suggestion anyways.August 3, 2021 at 7:23 pm #1882904Tom
Lead DeveloperLead DeveloperHi there,
Let’s try this instead:
// Track IDs added by WPSP. add_action( 'wpsp_before_wrapper', function() { global $displayed_posts; $displayed_posts[] = get_the_ID(); } ); // Don't output posts already being output. add_filter( 'generate_do_template_part', function( $do ) { global $displayed_posts; if ( in_array( get_the_ID(), (array) $displayed_posts ) { $do = false; } return $do; } );
Let us know ๐
August 4, 2021 at 10:50 am #1883969Malin
Hi Tom,
Thank you for trying, but they still show up twice. Maybe it’s something with the way I selected the posts. I selected by “tag” and everything tagged “news” first, then I tried selecting by category (many of them)… should I do something different there?
Appreciate your help.August 5, 2021 at 7:12 pm #1885658Tom
Lead DeveloperLead DeveloperStrange, does the following output any debug info on the site?:
// Track IDs added by WPSP. add_action( 'wpsp_before_wrapper', function() { global $displayed_posts; $displayed_posts[] = get_the_ID(); } ); // Don't output posts already being output. add_filter( 'generate_do_template_part', function( $do ) { global $displayed_posts; var_dump($displayed_posts); if ( in_array( get_the_ID(), (array) $displayed_posts ) { $do = false; } return $do; } );
August 6, 2021 at 2:01 am #1885879Malin
Hi Tom!
Thank you for helping!
I wasn’t sure what to do, so I replaced the first code (posted August 3, 2021 at 7:23 pm) with the one above, but got this:ParseError thrown
syntax error, unexpected ‘;’I then tried have the first piece active and also adding the new one, but I got the same error.
August 6, 2021 at 8:37 am #1886402David
StaffCustomer SupportCan you remove any of the code, you have added regarding this topic.
And save those changes. And hopefully there is no error.Then add just the code that Tom provided here:
August 7, 2021 at 6:23 am #1887075Malin
it gave me a parse error and broke the site.
August 7, 2021 at 6:57 am #1887094Malin
Siteground helped me deactivate the plugin, site is back up… ๐
I then deleted the code snippet plugin. Downloaded it again, but as soon as I activate it I get the parse error and the site breaks.
I figured I must have done something wrong, so I restored a backup from yesterday. The site is back up.
So, now the Code snippet is installed, but the only active snippet is this one:
https://docs.generatepress.com/article/disable-the-block-editor-in-widgets/
Nothing affecting the front page.Any other ideas I could try? ๐
August 7, 2021 at 7:14 am #1887119David
StaffCustomer SupportSo sorry about that. Just noticed a missing
)
in Toms code.Try this:
// Track IDs added by WPSP. add_action( 'wpsp_before_wrapper', function() { global $displayed_posts; $displayed_posts[] = get_the_ID(); } ); // Don't output posts already being output. add_filter( 'generate_do_template_part', function( $do ) { global $displayed_posts; var_dump($displayed_posts); if ( in_array( get_the_ID(), (array) $displayed_posts )) { $do = false; } return $do; } );
August 7, 2021 at 10:13 am #1887472Malin
Ah, what strange things can happen with a little “)”. ๐
Added the snippet, but no change the posts in the first 4 are still present below.
Thank you for trying though…
https://www.alternativephotography.com/I’m wondering if it has something to do with the changes I did to the WP show posts? This one:
Magazine Grid = WP show posts “Header”
I have selected show posts –> category and then about 180 categories, excluding workshop listings and suppliers.The Archive Header Block just says “Hello World”.
Could that be causing the issue?
Is there a way to say “show all posts but exclude workshop listings and suppliers” with category numbers or something similar?
Thank you for your perseverance!August 8, 2021 at 2:54 am #1887851David
StaffCustomer SupportWith that code added it – it should have displayed some code blocks on the frontend that contain this kind of thing:
array (size=1) 0 => int 44
Does it not output anything like that ?
August 8, 2021 at 7:34 am #1888008Malin
Hi David,
I think there is something wrong with the code… it generates a line above each post on the site, like this “array(1) { [0]=> int(43172) } array(1) { [0]=> int(43172) }”, see screenshot:August 8, 2021 at 7:54 pm #1888598Tom
Lead DeveloperLead DeveloperAha, so let’s try this instead:
// Track IDs added by WPSP. add_action( 'wpsp_before_header', function() { global $displayed_posts; $displayed_posts[] = get_the_ID(); } ); // Don't output posts already being output. add_filter( 'generate_do_template_part', function( $do ) { global $displayed_posts; if ( in_array( get_the_ID(), (array) $displayed_posts )) { $do = false; } return $do; } );
Let’s hope that does the trick. Let us know ๐
-
AuthorPosts
- You must be logged in to reply to this topic.