• Resolved hyperbytes

    (@hyperbytes)


    Hi everyone
    I am currently using the Dyad theme on a website which has an number of different post types i.e. news, events, advertisers.
    At present all post types are displayed on the home page however i would like to filter the front page posts to a specific post category i.e. news?
    I am struggling to work out how to do that.
    Any ideas greatly appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • By default, the home page will show all of your posts as you’ve seen, but you can add a filter in a child theme to change that behavior.

    First, set up your child theme. Once that is done, you’ll need to grab the ID number of the category in question.

    In your dashboard, go to Posts > Categories. Hover over the category you want and hover over the Edit link. Your browser should display the address for that link, which will include something like tag_ID=3 – that number is your category’s ID.

    Next, add the following snippet to your child theme’s functions.php theme:

    function homepage_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '3');
        }
    }
    add_action( 'pre_get_posts', 'homepage_category' );

    On the third line of that code, you’ll want to swap in your own ID number, instead of the 3 in my example.

    That should do the trick!

    Thread Starter hyperbytes

    (@hyperbytes)

    Thanks Chad, problem solved!

    You’re welcome!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filtering posts on front page’ is closed to new replies.