[resolved] How can we list posts from single category on posts page? (5 posts)

  1. Keith Ratner
    Posted 1 year ago #

    On the 'Posts Page' selected under 'Front Page' options, is there a snippet available to force that page to only display posts from a specific category, or even multiple categories?

  2. acub
    Posted 1 year ago #

    add_filter('pre_get_posts', 'limit_homepage_posts');
    function limit_homepage_posts($query) {
    	if ( $query->is_home() && $query->is_main_query() )
    		$query->set( 'cat', 'X,Y' );

    Replace X,Y with category ids. Or, if you want to use slugs, use

    $query->set( 'category_name', 'slug1,slug2' );

    This function (and the preceding filter) should go in functions.php of your child theme.

  3. Keith Ratner
    Posted 1 year ago #

    Perfect. Thanks!

  4. afizan
    Posted 1 year ago #

    Hi I am a bit confuse on how the filter function. Could you elaborate a little bit more please.

    Did I need to create a new page with the same nae of category?

  5. acub
    Posted 1 year ago #

    You don't need to create a template. The function above filters the posts displayed on homepage.

    More on how filters work in WP here. It's what makes WP flexible.

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic