Site logo

Archived topic

Filter for home items

8 replies · Started by JORGE on February 3, 2021

Viewing posts 1–9 of 9

Hello, I would like to know if there is any way to filter the entries that are shown in the home.

My site is

You can see in the Home that there are 4 featured items in carousel form, but then then they are displayed again in the list.

Items featured in the header are a GP element, using the plugin wp_show_posts

Is there a way to have a filter that prevents showing the last 4 articles published in the post list home? This is how the 4 most recent ones remain on the carousel and the others on the list.

Thanks a lot!

Hi JORGE,

Did you forget to link the site? :)

Hi, the problem is that those 4 publications that are seen in a carousel are the 4 most recent, as they are published they move automatically.

Try this PHP snippet:

add_filter( 'pre_get_posts', function( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $offset = 4;
        $ppp = get_option('posts_per_page');

        if ( $query->is_paged ) {
            $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
            $query->set('offset', $page_offset );
        } else {
            $query->set('offset',$offset);
        }
    } else {
        return $query;
    }
} );

Adding PHP: https://docs.generatepress.com/article/adding-php/

It works perfect, thanks a lot!

Hi, the filter has a problem and that is that there are times when it does not show some articles.

In the carousel only articles of 2 categories are shown, the others never go through the carousel, they are printed directly in the list. The problem is that with the code snippet those articles that we do not print in the carousel are not printed in the list either, they remain hidden until more are published.

Hi Jorge,

Unfortunately your request would require modifying the WordPress post loop pretty extensively which is outside of the forum support scope.

You could try checking with WordPress' support team and see if they are able to help or open a topic in general WordPress forum like this:
https://wordpress.stackexchange.com/

The post loop is completely handled by WordPress itself so if the code works in a Twenty series WP theme, the exact code would work in GeneratePress as well.

This archived topic is closed to new replies.