• My WP theme has few sections in homepage. By default, each section contains the last posts.

    These are the first 3 sections in the homepage:
    1. featured posts 1
    2. featured posts 2
    3. header featured posts

    I got the code that removes duplicated posts from the second section (see below).
    How can I extend the code in order to remove duplicated last posts from the third section as well? (not a programmer)

    This is the code I got (and works!):

    if ( $newscard_settings['newscard_banner_featured_posts_1_hide'] === 0 ) {
        $featured_post_1_ids = wp_list_pluck( $newscard_get_featured_post_1->posts, 'ID' );
        $post_type_fp_2['post__not_in'] = array_unique( array_merge( $post_type_fp_2['post__not_in'], $featured_post_1_ids ) );
    }
    • This topic was modified 4 years, 5 months ago by bcworkz. Reason: code fixed
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    You need to affect the third query in the same manner. Pluck IDs from the results of the second query that uses $post_type_fp_2 args. You can array_merge() any number of arrays. Merge the ‘post__not_in’ arg array element (if it exists) from the third query’s args with $post_type_fp_2[‘post__not_in’] and the IDs plucked from the second query. Get unique IDs from the merge and assign to the third query’s ‘post__not_in’ arg array element.

    There’s not enough context to give you anything more specific.

    Thread Starter Shir

    (@shirwp)

    @bcworkz thanks for your kind reply!

    1. Where do I get the pluck IDs from? As well as the $post_type_fp_2 args? Is it in header.php file?
    2. What’d be a good context? Does attaching header.php code here as a file be enough?

    Thanks again ??

    Moderator bcworkz

    (@bcworkz)

    You pluck IDs from the second query object’s posts property after it has been instantiated. I don’t know where that would be. On the page’s templates somewhere perhaps. Unlikely to be header.php, though it’s remotely possible. It could even be in a shortcode handler or block code. Where does the code you have live? How/where is the second/third section’s posts output? The context I’m looking for is whatever code is responsible for querying for the posts in each section. That can be in any number of places. The query itself is usually done with get_posts or new WP_Query. Possibly query_posts.

    If you find the need to post a large amount of code, please post it at pastebin.com or gist.github.com and provide the link here.

    Thread Starter Shir

    (@shirwp)

    @bcworkz Again, thanks so much for your time ??

    OK, so I just searched (simply by ctrl+f) what you’ve mentioned above (get_posts, new WP_query, and query_posts) inside most of the theme files. Couldn’t find anything.
    The only thing I was able to find is get_post (singular) inside some template-parts file.

    If that can help – this is my theme files structure (where can it possibly be?):

    Theme:
    assets
    css
    customize-controls.css
    js
    customizer-control.js
    customizer.js
    html5.js
    scripts.js
    skip-link-focus-fix.js
    library
    many different sub-files ??
    inc
    tgm
    class-tgm-plugin-activation.php
    theme-info (many files under this one as well)
    custom-header.php
    customizer.php
    functions.php
    newscard-footer-info.php
    newscard-metaboxes.php
    newscard-widgets.php
    template-functions.php
    languages
    template-parts
    templates
    404.php
    arcieve.php
    comments.php
    footer.php
    functions.php
    index.php
    LICENSE
    page.php
    readme.txt
    rtl.css
    screenshot.png
    search.php
    searchform.php
    sidebar.php
    single.php
    style.css
    wpml-config.xml

    • This reply was modified 4 years, 5 months ago by Shir.
    • This reply was modified 4 years, 5 months ago by Shir.
    Moderator bcworkz

    (@bcworkz)

    It could be in any of those numerous files in subfolders. Searching individual files with Ctrl-F is way too tedious to reliably find anything. You need a tool that searches for text in multiple files in an entire directory tree. The classic Unix/Linux command line tool that does this is grep. On Windows you can use findstr to the same end. Download your theme’s files to your local computer (best as close as possible to C:\, avoid spaces in path folder names). Get to the Windows command prompt (search “command” in the Windows search box). Use cd at the prompt to make your theme’s folder current. Enter something like findstr /S /N /C:"new WP_Query" *.php. This will search all .php files in the current folder and all of its subfolders.

    This could yield too many results to be useful.

    Thread Starter Shir

    (@shirwp)

    OK.
    So the third section (that repeat posts from sections 1+2) is in WP under customizing –> “header featured posts”.

    Beside that, I’ve searched “new WP query” as you guided me above – there are multiple results.
    Not sure which one is the relevant…

    Here are the results (again, saved in Github as you guided me :)) :
    https://github.com/shirgu/remove-duplicate-hp-posts

    Moderator bcworkz

    (@bcworkz)

    The search results alone would not be enough to determine which is applicable. It’s only a start for where to investigate further. “header featured posts” in the customizer is something added by your theme. I still wouldn’t know where the related query is made.

    It’s possible to alter queries through the “pre_get_posts” action hook without knowing where the query is instantiated, but you still need to know something distinctive about the query so only the correct one is modified. This may not be any more useful, but it’s an option if the right information is known.

    I recommend asking how to modify the query through your theme’s dedicated support channel. They’ll know better than anyone how to best do that.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Remove duplicate posts in different homepage sections’ is closed to new replies.