How to exclude specific pages from Wordpress search results - wordpress

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/

Related

Can I exclude a specific category of a custom post type from both search and archives?

I have a custom post type "courses" and the taxonomy for categories within this post type is "course-category."
I have a course category called "Academy" and I would like to exclude that category from any search, and from any archive pages for that custom post type.
Right now I have this code snippet but it doesn't seem to be working. It's for the search exclusion only but I need to exclude the category in both search pages and archive pages.
function wpb_search_filter( $query ) {
if ( $query->is_search && !is_admin() )
$query->set('post_type', array('courses');
$query->set( 'course-category','-68' );
return $query;
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );
Have you tried using is_post_type_archive()?
function wpb_search_filter( $query ) {
if ( $query->is_search && !is_admin() || $query->is_main_query() && !is_admin() && $query->is_post_type_archive('courses') ) {
$query->set('post_type', array('courses');
$query->set( 'course-category','-68' );
return $query;
}
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );

Display a number of post per page in custom taxonomy page in Wordpress

I have the following function in my functions.php, that changes the number of posts displayed per page in the custom post type archive page. How can I also target the corresponding taxonomy page(taxonomy-case.php)? Is there something similar to "is_post_type_taxonomy"?
// Post number limits
function my_cptui_change_posts_per_page( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_post_type_archive( 'case' ) ) {
$query->set( 'posts_per_page', 8 );
}
}
add_filter( 'pre_get_posts', 'my_cptui_change_posts_per_page' );
Add below code inside your function and check if it's working. Don't forget to replace your category with your taxonomy name.
if (is_tax( 'your-category' ) ) {
$query->set( 'posts_per_page', 8 );
}
The is_tax() function should accomplish what you're looking to do.
// Post number limits
function my_cptui_change_posts_per_page( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_post_type_archive( 'case' || is_tax( 'case' ) ) ) { // Or whatever your taxonomy name is
$query->set( 'posts_per_page', 8 );
}
}
add_filter( 'pre_get_posts', 'my_cptui_change_posts_per_page' );

Hiding category posts from blog section in Wordpress

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');

WordPress WooCommerce after theme setup not working

I just want to customize the theme of WooCommerce in my theme. But the problem is when I add the folder of WooCommerce template to override the WooCommerce theme and after adding this function:
function add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
add_action( 'after_setup_theme', 'add_woocommerce_support' );
The shop page is not displaying the list of products. Instead the archive post is displaying. But when I comment the add_action of woocommerce theme setup, everything is working fine. See this picture:
I also have this function to display my custom post type all post in archive page:
function add_cpt_program_in_archive( $query ) {
if ( !is_admin() && $query->is_archive() && $query->is_main_query() ) {
$query->set( 'post_type', array('post', 'program' ) );
}
}
add_action( 'pre_get_posts', 'add_cpt_program_in_archive' );
I also try to add !is_shop() in the condition of my add_cpt_program_in_archive function. So its look like this:
function add_cpt_program_in_archive( $query ) {
if ( !is_admin() && $query->is_archive() && $query->is_main_query() && !is_shop() ) {
$query->set( 'post_type', array('post', 'program' ) );
}
}
add_action( 'pre_get_posts', 'add_cpt_program_in_archive' );
The shop page is now displaying the list of products. But the problem is when I visit the category page of product. Example the uncategorized category. It's not displaying the list of product that was in the uncategorized category. See this picture:
I found the solution. I add this code !$query->query_vars['product_cat']) in the condition of my add_cpt_program_in_archive function. So its look like this
function add_cpt_program_in_archive( $query ) {
if ( !is_admin() && $query->is_archive() && $query->is_main_query() && !is_shop() && !$query->query_vars['product_cat']) {
$query->set( 'post_type', array('post', 'program' ) );
}
}
add_action( 'pre_get_posts', 'add_cpt_program_in_archive' );
But I dont know if this is correct format or maybe you guys have a better suggestion answer rather than to my solution.

How to wordpress random posts in tag.php?

I want to show random articles on the tags page on my wordpress site, how can I do?
I did a few edits to the functions file, but it didn't work
function one_random_post_on_home_page( $query )
{
if ( ! ( $query->is_tag() && $query->is_main_query() ) )
return;
$query->set( 'orderby', 'rand' );
$query->set( 'posts_per_page', 12 );
}
add_action( 'pre_get_posts', 'one_random_post_on_home_page' );
again only one article appeared.
Your return statement is ending the execution of the function before your edits to the query. Also, your conditional is looking for anything but the tag archive and main query by using the ! in your code. The solution below is working for me.
function randomize_tag_archive( $query ){
if ( $query->is_tag() && $query->is_main_query() && !is_admin() ) {
$query->set( 'orderby', 'rand' );
$query->set( 'posts_per_page', 12 );
}
}
add_action( 'pre_get_posts','randomize_tag_archive' );

Resources