• I created a patch that prevents repeating the same post on the same page. I’m using this on my home page as I have 4 instances of the widget in different sidebars (the way the template is laid out), and I don’t want repeats.

    In my case, I have one widget showing the “latest” post, then the other 3 show the latest from specific categories. With this patch, if the newest happens to be in category A, then the category A column will show the second newest. If I have a post that happens to be in multiple categories, it too will not be repeated.

    It’s a bit crude because it’s using global variables — but that is the WordPress way I guess, and echos what the plugin already does. ??

    I did not make a UI to enable this behaviour, though I certainly could if this patch will be accepted.

    Index: class-recent-posts-widget.php
    ===================================================================
    --- class-recent-posts-widget.php       (revision 580132)
    +++ class-recent-posts-widget.php       (working copy)
    @@ -66,6 +66,10 @@
                    $instance['terms'] = apply_filters( 'frp_terms', $instance['terms'] );
                    $instance['all_posts_title'] = htmlspecialchars( $instance['all_posts_title'] );
    
    +               // exclude posts we've already shown
    +               $instance['dont_repeat'] = true; // TODO: make a configuration setting
    +               $exclude_posts = ($instance['dont_repeat'] && is_array($GLOBALS['frp_posted_ids'])) ? $GLOBALS['frp_posted_ids'] : array();
    +
                    if ( !empty( $instance['terms'] ) || !empty( $instance['all_terms'] ) ) {
                            $tax = key( empty( $instance['terms'] ) ? $instance['all_terms'] : $instance['terms'] );
                            $terms = ( empty( $instance['all_terms'] ) ) ? current( $instance['terms'] ) : get_terms( $tax, array( 'fields' => 'ids' ) );
    @@ -73,6 +77,7 @@
                            $query = array(
                                    'numberposts' => $instance['number'],
                                    'post_type'   => 'any',
    +                               'post__not_in'=> $exclude_posts,
                                    'tax_query'   => array(
                                            array(
                                                    'taxonomy' => $tax,
    Index: templates/recent-posts-widget.php
    ===================================================================
    --- templates/recent-posts-widget.php   (revision 580132)
    +++ templates/recent-posts-widget.php   (working copy)
    @@ -18,6 +18,9 @@
                    foreach ( $posts as $post ):
                            // Replace global post variable with current looped post.
                            $GLOBALS['post'] = $post;
    +
    +                       // remember that we posted this
    +                       $GLOBALS['frp_posted_ids'][] = $post->ID;
                            ?>
                            <li class="frp-news">
                                    <?php echo wpautop( do_shortcode( $instance['template'] ) ); ?>

    https://www.ads-software.com/extend/plugins/flexible-recent-posts/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Paul Annekov

    (@thesteelrat)

    Can you explain your case with 4 widgets more detailed? I don’t understand what do your widgets display and how many posts do they have?

    As I see from your patch – you simply save all displayed posts in global variable and then exclude these IDs in next widget’s query. But explain me the case where it may be necessary.

    Thread Starter gregmac

    (@gregmac)

    Here’s an example: https://i.imgur.com/alcZQ.png

    So there are 4 widget areas, and each have just the FRP widget in them set to display one post.

    * Featured: Displays the latest post
    * Head1: Displays the latest post from category A
    * Head2: Displays the latest post from category B
    * Head3: Displays the latest post

    Sometimes I post to cat A, sometimes B, and sometimes to other (or no) categories. Without this patch, here’s the issue:

    * If I post with no category, Featured and Head3 show the same thing
    * If I post to category A, Featured, Head1 and Head3 show the same thing
    * If I make a post in both cat A and B, all 4 areas show that same post

    With this patch:

    * If I post to no category, Featured shows that post, and Head3 shows the second-latest post provided it is not in Head1 or Head2
    * If I post to category A, Featured shows that post, Head1 shows the next-latest post in category A, and Head3 shows the next-latest post that is not in cat A or B
    * Even if I make a post in both cat A and B, I’m still guaranteed that it will only ever show up once

    What I was looking for was a way to have two distinct “sections” on the site (cat A and B), and also be able to post outside of those categories, and still bring them together in a nice way.

    —-

    I guess here’s another way to look at it. If you only use one FRP widget, this patch will not affect the behaviour at all. If you use more than one FRP widget, this guarantees no duplication — so the only people affected are ones that use multiple FRP’s on a page but actually want duplication. I am not sure what the use case there is, but if there is a legitimate one, it’s relatively straight-forward to add a config setting to keep the existing behaviour.

    Plugin Author Paul Annekov

    (@thesteelrat)

    Ok, I will think about this feature. At least I will add more filters and actions, so everybody can make it’s own plugin, that will extend the functionality of FRP.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Plugin: Flexible Recent Posts] [patch] Don't repeat posts’ is closed to new replies.