We need to hide a specific category type on all pages but one, and then display that hidden category on a singular page.
So far, to hide the category on all pages, we have coded this:
// Exclude Category Posts from Home Page
function themeprefix_exclude_category( $query ) {
if ( $query->is_home() ) {
$query->set( 'cat', '-3' );//add your category number
}
return $query;
}
add_action( 'pre_get_posts', 'themeprefix_exclude_category' );
We have found this code to display only a certain post category, but it does not work for a specific page.
// Only Portfolio Category
function only_portfolio_category( $query ) {
if ( $query->is_page('clinical-trials') && $query->is_main_query() ) {
$query->set( 'cat', '3' );
}
}
add_action( 'pre_get_posts', 'only_portfolio_category' );
If anyone has a workaround for this, we'd love to know!
We need to hide a specific category type on all pages but one, and then display that hidden category on a singular page.
So far, to hide the category on all pages, we have coded this:
// Exclude Category Posts from Home Page
function themeprefix_exclude_category( $query ) {
if ( $query->is_home() ) {
$query->set( 'cat', '-3' );//add your category number
}
return $query;
}
add_action( 'pre_get_posts', 'themeprefix_exclude_category' );
We have found this code to display only a certain post category, but it does not work for a specific page.
// Only Portfolio Category
function only_portfolio_category( $query ) {
if ( $query->is_page('clinical-trials') && $query->is_main_query() ) {
$query->set( 'cat', '3' );
}
}
add_action( 'pre_get_posts', 'only_portfolio_category' );
If anyone has a workaround for this, we'd love to know!
Share Improve this question edited Oct 10, 2019 at 21:40 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Oct 10, 2019 at 21:03 RicoRico 11 Answer
Reset to default -1Try this using is_single and query_vars rather than is_page:
add_action( 'pre_get_posts', 'change_queries', 1000 );
function change_queries ($query){
if(is_single() && $query->query_vars['page_id']== 'your page id'){
$query->set( 'cat', '3' );
}
}