This PHP is how to exclude categories of posts from displaying in a specific page. Please place this PHP code below in functions.php of your theme directory to cause your page to display posts only from some categories.
PHP – Filter Posts Category Of A Page In WordPress
For example we want to display only post categories with ID: 62,51,4 and 52 in a page has ID 118.
function ops_exclude_category( $query ) { if ( $query->queried_object->ID == 118) { $query->set( 'cat', '62,51,4,52' ); } } add_action( 'pre_get_posts', 'ops_exclude_category' ); |
In order to get more examples and have a deeper understand, please go to WordPress Plugin API/Action Reference/pre get posts