I'm trying to hide an entry from a blog section in wordpress. I have done the typical modification on function.php which is
function exclude_category( $query ) {
if ( !$query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-114' );
}
return $query;
}
add_action( 'pre_get_posts', 'exclude_category' );
And the category is still there. What I am doing wrong?. Thanks in advance
Try with only is_home condition.
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', 'YOUR-CAT-ID' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
Can check the condition is correct for is_home and is_main_query
function exclude_category($query) {
$query->set('cat','-114');
return $query;
}
add_filter('pre_get_posts','exclude_category');
Related
I have some pages that I don't want it to be included when anybody searches within my website. I tried to add the code below to my function.php but it doesn't work
add_filter( 'pre_get_posts', 'exclude_pages_search_when_logged_in' );
function exclude_pages_search_when_logged_in($query) {
if ( $query->is_search && is_user_logged_in() )
$query->set( 'post__not_in', array( 6410, 1684, 6385, 278, 6390, 865 ) );
return $query;
}
How do I exclude some pages from the search results?
add_action is what you use to create a trigger “hook” – when something happens, do-something-else.
add_filter is used to “hook” data change/replace. use the code below instead
add_action( 'pre_get_posts', 'my_search_exclude_filter' );
function my_search_exclude_filter( $query ) {
if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
$query->set( 'post__not_in', array( 9564, 1213 ) );
}
}
I also described the other solutions is my blog post https://naderzad.info/web-development/wordpress-search-result/
I have created a Page Templates in WordPress. The body comes in the class as the following image. I want to remove 'page' class.
You can add or remove class from body tag with the help of filter. try below script.
function remove_body_classes( $wp_classes ) {
$blacklist = array( 'page' );
$wp_classes = array_diff( $wp_classes, $blacklist );
return $wp_classes;
}
add_filter( 'body_class', 'remove_body_classes', 10, 2 );
You should add this script at the end of the Page Template that you have created .
<script type="text/javascript">
$=jQuery;
$(document).ready(function(){
$( "body" ).removeClass( "page" )
});
</script>
Filter body_class using array_search as follow works for me:
add_filter( 'body_class', function( $classes ) {
unset($classes[array_search('page', $classes)]);
return $classes;
});
add_filter( 'body_class', function( $classes ) {
if ( isset( $classes['page'] ) ) {
unset( $classes['page'] );
}
return $classes;
});
Sorry, I wanted to do the particular for the one page.I have manually adopted the solution which is as follows.The directory path in my condition was wrong
function remove_body_classes( $wp_classes ) {
if ( is_page_template( 'inc/home-template-page.php' ) ) {
$page = array( 'page' );
$wp_classes = array_diff( $wp_classes, $page );
}
return $wp_classes;
}
add_filter( 'body_class', 'remove_body_classes', 10, 2 );
I am looking for a solution in order to remove /%category%/ from my permalinks for only one category ("general").
My permalink is currently set to /%category%/%postname%/. How can I create the following URLs only for the "general" category /%postname%/ ?
Thanks!
You can set custom structure in Permalinks Settings: Dashboard - Settings>Permalinks
This works for me:
function remove_uncategorized( $permalink, $post, $leavename ) {
if( $post->post_type != 'post' ) return $permalink;
$cats = get_the_category($post->ID);
if( ! count($cats) ) return $permalink;
usort($cats, '_usort_terms_by_ID');
$category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
$category_object = get_term( $category_object, 'category' );
return _clear_uncategorized($category_object, $permalink);
}
function _clear_uncategorized($cat, $permalink) {
if( $cat->slug == 'sin-categoria' ) {
return str_replace('%category%/', '', $permalink);
}
$parent = $cat->parent;
if ( !$parent )
return $permalink;
return _clear_uncategorized($parent, $permalink);
}
add_filter( 'pre_post_link', 'remove_uncategorized', 9, 3 );
I have a problem when trying to get the search to work for both the default WordPress blog search and WooCommerce product search and I can't seem to find the solution anywhere. What I have in functions.php right now is:
function wp_search_filter($query) {
if ( $query->is_search ) {
$query->set( 'post_type', 'post' );
}
if ( function_exists( 'is_woocommerce' ) ) :
if ( $query->is_search && is_woocommerce() ) {
$query->set( 'post_type', 'product' );
}
endif;
return $query;
}
add_filter('pre_get_posts','wp_search_filter');
But my product search does not work. Any ideas?
To answer my own question, in case anyone needs a solution, I just came up with something that works. The problem here was that the is_woocommerce() condition is not firing on search results template. To get around that I used this code:
function wp_search_filter($query) {
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( (strpos($url,'post_type=product') !== false) && is_search() ) {
$query->set('post_type', 'product');
}
return $query;
}
add_filter('pre_get_posts','wp_search_filter');
I am trying yo Remove custom body class from pages with a specific category in WordPress.
Here is the code below I am trying to make to work. However, it does not.
function remove_body_class($wp_classes) {
if ( is_category ('places') ) :
foreach ( $wp_classes as $key=>$value ) {
if ( $value =='my_class' ) unset( $wp_classes[ $key ] );}
endig;
return $wp_classes;
} add_filter( 'body_class', 'remove_body_class');
It works when I remove class from all pages without using "if ( is_category ('places') ) :"
But I can't make it work only for specific category/posts.
Could you tell me if I do something wrong? I would highly appreciate it.
Thank you.
it will be help for you.
// Removes a class from the body_class array.
add_filter( 'body_class', function( $classes ) {
if ( isset( $classes['your-class-name'] ) ) {
unset( $classes['your-class-name'] );
}
return $classes;
} );
// 34 is your category id
if (is_category('34'))
{
add_filter( 'body_class', function( $classes ) {
if ( isset( $classes['your-class-name'] ) ) {
unset( $classes['your-class-name'] );
}
return $classes;
} );
}
// When the archive page for Category 34 is being displayed.
Define $cat_id to the category ID and change "your-class-name" to the name of the class you want removed:
if ( is_category($cat_id) ) {
add_filter( 'body_class', function( $classes ) {
if ( null !== array_search( 'your-class-name', $classes) ) {
unset( $classes[ array_search( 'your-class-name', $classes) ] );
}
return $classes;
} );
}