We're merging with GenerateBlocks! Learn more here.

[Resolved] Stop showing duplicate posts on the same page/post using a different shortcode

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Stop showing duplicate posts on the same page/post using a different shortcode

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #15628
    Anna
    Participant

    Hi WP Show Posts,

    I’m new to this plugin and just bought the Pro version. I use WP Show Posts to design my homepage (I create a page and point my homepage to it).

    Because my page uses several shortcodes, I end up seeing duplicate posts outputted. This happens because many of my posts are in more than 1 category. I also like to have a “Latest Posts” section which outputs 2 posts based on Modified Date. Inevitably, the posts will show up again later down the page in their category’s section. Can I configure the plugin to tell the shortcodes to stop outputting any posts that have been included by other instances of the shortcode on the same page or post (eg. previously in the post, the sidebar, the footer)?

    Here are more details in case it helps:

    Steps to Reproduce

        Create 5 new posts and categorize them accordingly (from the oldest to the newest created post): 1. Chocolate cake recipe (category: recipe, baking), 2. Blueberry Muffin recipe (category: recipe, baking), 3. How to buy baking pans (category: baking, equipment), 4. How to use an oven (category: baking, equipment), 5. Scones recipe (category: recipe)
        Create 3 lists. List 1 outputs 2 posts using Modified in descending order (it shows the 2 latest posts published, not filtered by category). List 2 outputs 2 posts in the baking category using Modified in descending order. List 3 outputs 2 posts in the recipe category using Modified in descending order.
        Create a new page. Put the shortcodes on the page with their relevant headings, e.g.

        Latest posts
        [List1]
        Learn about baking
        [List2]
        Get the best recipes
        [List3}

    Actual Results
    The homepage will look like this:

    Latest Post
    Scones recipe, How to use an oven

    Learn about baking
    How to use an oven, How to buy baking pans

    Get the best recipes
    Scones recipe, Blueberry muffins recipe

    Desired Results
    I want the shortcodes to not output any posts that have been included by other instances of the shortcode. This is my desired homepage:

    The homepage ideally looks like this:

    Latest Post
    Scones recipe, How to use an oven

    Learn about baking
    How to buy baking pans

    Get the best recipes
    Blueberry muffins recipe, Chocolate cake recipe

    Workaround
    I’m currently getting around the duplication by using order by Random in some of my categories to avoid outputting the same posts. However, I don’t like this workaround because it often outputs my oldest lower-quality posts which embarrassing to have on the homepage because it’s not my best work. I don’t deem it to be a good use of my time to go back to fix old posts that don’t generate much traffic just so they will display nicely on the homepage. I’d prefer to show the latest modified posts which have the best quality content I can offer my readers.

    Is it possible for you to include this feature in the WP Show Posts plugin? If not, can you give me the PHP code to produce my desired behavior?

    Thanks.

    #15630
    Anna
    Participant

    In case you want to see what my live homepage looks like, here’s a link: https://garlicdelight.com

    #15645
    Tom
    Keymaster

    Hi Anna,

    This is a tough one – I’m not sure it’s going to be possible to make the loops communicate with each other.

    I do have one idea, but it’s a long shot. We’ll have to do this in a couple of steps to debug a lot the way:

    add_filter( 'wp_show_posts_shortcode_args', function( $args ) {
        global $added_ids;
        var_dump($added_ids); // This should maybe output a list of all IDs output on the page. Does it?
    
        return $args;
    } );
    
    add_action( 'wpsp_before_header', function() {
        global $added_ids;
    
        $added_ids[] = get_the_ID();
    } );

    Does that output anything at all above each list?

    #15658
    Anna
    Participant

    Yes it outputs the following in front of the first image/item in the list.
    array(0) { }

    Let me know the next step you want me to run.

    By the way, I have a paid plugin that achieves this desired behavior using shortcodes. You can read about it: https://feastdesignco.com/fsri-shortcode/
    But I prefer the GeneratePress framework so I’d like to switch to WP Show Posts if you think we can replicate the same behavior

    #15680
    Tom
    Keymaster

    Right now it’s not a feature, unfortunately. However, I made a tweak to the code above that might work: https://wpshowposts.com/support/topic/stop-showing-duplicate-posts-on-the-same-page-post-using-a-different-shortcode/#post-15645

    Let me know if that outputs something different.

    #15690
    Anna
    Participant

    Hi Tom,

    Here’s the output in the order from top to bottom of the page:

    NULL

    array(2) { [0]=> int(4737) [1]=> int(4421) }

    array(5) { [0]=> int(4737) [1]=> int(4421) [2]=> int(4253) [3]=> int(3760) [4]=> int(3745) }

    array(8) { [0]=> int(4737) [1]=> int(4421) [2]=> int(4253) [3]=> int(3760) [4]=> int(3745) [5]=> int(4421) [6]=> int(4413) [7]=> int(4495) }

    array(11) { [0]=> int(4737) [1]=> int(4421) [2]=> int(4253) [3]=> int(3760) [4]=> int(3745) [5]=> int(4421) [6]=> int(4413) [7]=> int(4495) [8]=> int(4737) [9]=> int(4561) [10]=> int(4439) }

    array(14) { [0]=> int(4737) [1]=> int(4421) [2]=> int(4253) [3]=> int(3760) [4]=> int(3745) [5]=> int(4421) [6]=> int(4413) [7]=> int(4495) [8]=> int(4737) [9]=> int(4561) [10]=> int(4439) [11]=> int(4056) [12]=> int(3560) [13]=> int(3398) }

    array(17) { [0]=> int(4737) [1]=> int(4421) [2]=> int(4253) [3]=> int(3760) [4]=> int(3745) [5]=> int(4421) [6]=> int(4413) [7]=> int(4495) [8]=> int(4737) [9]=> int(4561) [10]=> int(4439) [11]=> int(4056) [12]=> int(3560) [13]=> int(3398) [14]=> int(4737) [15]=> int(4383) [16]=> int(4325) }

    #15716
    Tom
    Keymaster

    That looks promising! Let’s try this:

    add_filter( 'wp_show_posts_shortcode_args', function( $args ) {
        global $added_ids;
    
        // Exclude posts that have already been added.
        $args['post__not_in'] = $added_ids;
    
        return $args;
    } );
    
    add_action( 'wpsp_before_header', function() {
        global $added_ids;
    
        $added_ids[] = get_the_ID();
    } );
    #15728
    Anna
    Participant

    Hi Tom,

    I think your latest function worked!

    It’s amazing 😀
    I had 2 recipes duplicated 3 times and now I can’t find any sign of them (except for the footer… that’s expected because I specified the post ID in the list for those Reader Favorites posts).

    Wow, this level of support and functionality blows my mind compared to what I’ve experienced before in the WordPress ecosystem. Thank you very much! Very happy customer.

    Anna

    #15748
    Tom
    Keymaster

    Awesome! Really happy I was able to get that working for you 🙂

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