Archived topic
Dispatch theme: Posts showing up twice on homepage
26 replies · Started by Malin on August 1, 2021
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!
Hi 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. :D
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.
Hi 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 :)
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.
Strange, 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;
} );
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.
Can 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:
it gave me a parse error and broke the site.
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? :-)
So 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;
} );
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!
With 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 ?
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:
Aha, 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 :)