Prevent WordPress search from search a specific pages parentpages - wordpress

I have a certain page in WordPress that have parent pages. I want to exclude those parent pages from WordPress search.
In functions.php I have tried this:
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_parent', '4');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
Well with this code only the post_parent is searchable, but I want the opposite. How would this look like?
UPDATE: Problem solved. Here's the solution (4 is the ID of the specific page where parent pages is to be excluded from search):
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_parent__not_in', array(4));
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
Kind regards
Johan

With Wordpress 3.6, can use new query param: post_parent__not_in
post_parent__not_in (array) - use post ids. Specify posts whose parent is not in an array.

Use This code function in your Theme's function.php file with page id which is you want to exclude from your Theme's custom search bar...And Enjoy with it...!
// Exclude specific posts/pages from search
function exclude_pages_from_search($query) {
if ( $query->is_main_query() && is_search() ) {
$exclude_ids = array(11);// Array of the ID's to exclude
$query->set( 'post__not_in', $exclude_ids );
}
return $query;
}
add_filter('pre_get_posts','exclude_pages_from_search' );

Related

Search Widget in Blog page in wordpress

I have added search widget on my Blog page. when I click on blank search it filters out all the page of my website. I want to add setting using coding that it only filters out the blog post.
I have added this code for that
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
}
}
add_action('pre_get_posts','search_filter');
but using this code, by search product functionality stop working.
Goto wp-includes/query.php
Find below and
$q[‘post_type’] = ‘any’;
replace
$q[‘post_type’] = ‘post’;
or better way to put this code in functions.php file to overcome the issue after any wordpress update
if (!is_admin()) {
function wpb_search_filter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','wpb_search_filter');
}

How to limit the search query and add filter in WordPress?

I am using a dynamix wordpress theme for new blog post website, when I try to filter posts, in the result page it is showing all the posts. I used many plugins as well to filter posts. such as search & filter and MDTF. still in the search result page it is showing all the available posts
Put below code in function.php file and your replace your posttype
function searchfilter($query) {
if ($query->is_search) {
$query->set('post_type', array( 'your-custom-post-type-name' ) );
}
return $query;
}
add_filter('pre_get_posts','searchfilter');

Remove category base from WordPress url only for specific category

I would like to remove the category base from Wordpress URL only for specific category.
For example, i need to change:
mysite.com/category/blog
to
mysite.com/blog
but i want to keep other categories unchanged:
mysite.com/category/songs
I think that it could be achieved with some .htaccess rule, but I found some generic rules that remove the basic category in all the url.
you can easily achieve this by using Enhanced Custom Permalinks Wp plugin. you just need to go edit the category, yo will see a field to add your custom url.
https://wordpress.org/plugins/enhanced-custom-permalinks/
This can be accomplished with some custom filters & actions.
Try placing this code in your theme's functions.php file:
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the categories for the post
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "News" ) {
$permalink = trailingslashit( home_url('/'. $post->post_name .'-'. $post->ID .'/' ) );
}
return $permalink;
}
add_action('generate_rewrite_rules', 'custom_rewrite_rules');
function custom_rewrite_rules( $wp_rewrite ) {
// This rule will will match the post id in %postname%-%post_id% struture
$new_rules['^([^/]*)-([0-9]+)/?'] = 'index.php?p=$matches[2]';
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite;
}
This will set up the permalink structure that you want for posts:
You can easily do that without using plugins.
Through Admin panel
go to settings->permalinks and select default to custom
here you know more..
https://codex.wordpress.org/Using_Permalinks

Wordpress advance search for function

I am trying to refine my wordpress search and found the following code that I placed into my functions.php which extends the wordpress search to include all pages and posts.
// Search Pages AND Posts
function filter_search($query) {
if ($query->is_search) {
$query->set('post_type', array('post', 'page'));
};
return $query;
};
add_filter('pre_get_posts', 'filter_search');
The above code is working and includes the pages into the searches now.:)
I also want to add the following if possible:
1.) Add my wordpress categories and tags to the above code so it will return searches that include them.
2.) Exclude certain pages that I don't want to so up in the search(such as: Terms and Condition page)
I know there are plugins that do stuff like this but I thought it may be less resources on my site if I could just extend the search functions.
Thanks
Greg
Categories are a separate taxonomy.
You'll have to create a add_filter for categories, or, just the child categories. And a function such as:
$categories = get_categories(‘child_of=1′);
$catlist = ”;
foreach ($categories as $cat) {
$catlist.= $cat->cat_ID.’,';
}
$catlist.’5′;
Then merge the $query, $categories vars
$mergedposts = array_unique( array_merge( $query, $categories ) );

Disable WooCommerce SKU on Product Page

I have a WooCommerce store and I don't want to display the SKU on any single product page. Looking at their code, I found this filter:
/**
* Returns whether or not SKUS are enabled.
* #return bool
*/
function wc_product_sku_enabled() {
return apply_filters( 'wc_product_sku_enabled', true );
}
and I attempted to override it with this line of code I placed in a custom plugin:
apply_filters( 'wc_product_sku_enabled', false );
I also tried placing the apply_filter inside an action function for woocommerce_product_meta_start which fires right before but it still renders the SKU on the product page. Any ideas?
I think you shoul try with this:
add_filter( 'wc_product_sku_enabled', '__return_false' );
That will remove sku from all woo, back and front end. You can always hide it just by CSS if need it on admin.
The easiest way is with CSS:
.sku_wrapper {
display:none;
}
A more robust approach is to recreate the woocommerce template woocommerce/templates/single-product/meta.php in your own theme and simply comment out the line:
<span class="sku_wrapper"><?php _e( 'SKU:', 'woocommerce' ); ?> <span class="sku" itemprop="sku"><?php echo ( $sku = $product->get_sku() ) ? $sku : __( 'N/A', 'woocommerce' ); ?></span>.</span>
To recreate a woocommerce template in your own theme, see:
http://docs.woothemes.com/document/template-structure/
Hiding the SKU/UGS by using cSS is not an efficient solution because it will be still part of the HTML code.
In order to hide it from the product single page and keep it in the admin page, you have to add this code in the child (or parent if you don’t have the child) functions.php :
// Remove the Product SKU from Product Single Page
add_filter( 'wc_product_sku_enabled', 'woocustomizer_remove_product_sku' );
function woocustomizer_remove_product_sku( $sku ) {
// Remove only if NOT admin and is product single page
if ( ! is_admin() && is_product() ) {
return false;
}
return $sku;
}
Make sure also in the product php page (it can have a different name depending on the theme you use) to have this condition to show the SKU in the product single page:
if (wc_product_sku_enabled() && $product->get_sku()) { // HTML code that shows the SKU in the product single page}
Make Sure to remove it from the frontend only by using this code on function.php usually you can edit the function file on theme editor
add_filter( 'wc_product_sku_enabled', 'my_remove_sku', 10 );
function my_remove_sku( $return, $product ) {
if ( !is_admin() && is_product() ) {
return false;
} else {
return true;
}
}
If you don’t need to use SKUs at all in your shop, you can disable them completely by using this plugin. simply install this plugin. https://wordpress.org/plugins/woocommerce-remove-sku/

Resources