Site logo

[Resolved] Dispatch theme: Posts showing up twice on homepage

Home Forums Support [Resolved] Dispatch theme: Posts showing up twice on homepage

Home Forums Support Dispatch theme: Posts showing up twice on homepage

Viewing 15 posts - 1 through 15 (of 27 total)
  • Author
    Posts
  • #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!

    #1880055
    Elvin
    Staff
    Customer Support

    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. ๐Ÿ˜€

    #1880526
    Malin

    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.

    #1882904
    Tom
    Lead Developer
    Lead Developer

    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 ๐Ÿ™‚

    #1883969
    Malin

    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.

    #1885658
    Tom
    Lead Developer
    Lead Developer

    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;
    } );
    #1885879
    Malin

    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.

    #1886402
    David
    Staff
    Customer Support

    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:

    https://generatepress.com/forums/topic/dispatch-theme-posts-showing-up-twice-on-homepage/#post-1885658

    #1887075
    Malin

    it gave me a parse error and broke the site.

    #1887094
    Malin

    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? ๐Ÿ™‚

    #1887119
    David
    Staff
    Customer Support

    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;
    } );
    #1887472
    Malin

    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!

    #1887851
    David
    Staff
    Customer Support

    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 ?

    #1888008
    Malin

    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:

    https://snipboard.io/l4jyPk.jpg

    #1888598
    Tom
    Lead Developer
    Lead Developer

    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 ๐Ÿ™‚

Viewing 15 posts - 1 through 15 (of 27 total)
  • You must be logged in to reply to this topic.