Search Widget in Blog page in wordpress - 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');
}

Related

Disable Elementor on specific page

I have a plugin that uses the default woocommerce my account shortcode and displays the page only for a specific role. But, I have customized the My Account page using elementor & crocoblock, and the plugin is not working properly. They did give a workaround for this in the documentation, but I can't seem to make it work. This is the code
add_filter( 'elementor/frontend/the_content', 'wcb2bsa_disable_elementor_on_sales_agents_area' );
function wcb2bsa_disable_elementor_on_sales_agents_area( $content ) {
if ( is_account_page() && is_user_logged_in() && wcb2bsa_has_role( get_current_user_id(), 'sales_agent' ) ) {
return '[woocommerce_my_account]';
}
return $content;
}
Is it because the elementor/frontend/the_content part is changed?

Wordpress Plugin Custom Post Type Archive Template results in other pages not displaying

This is the code I am using to call my archive template, now the knowledgebase section works, the forums work, but all other pages throughout the website are now blank.
function kb_archive_template_function($arhive_template){
if(is_post_type_archive('knowledgebase')){
$theme_files = array('/templates/archive-knowledgebase.php');
$exists_in_theme = locate_template($theme_files, false);
if($exists_in_theme == ''){
return plugin_dir_path(__FILE__) . '/templates/archive-knowledgebase.php';
}
return $archive_template;
}
}
You can use this code ,instead of your code
function get_custom_post_type_template( $archive_template ) {
global $post;
if ( is_post_type_archive ( 'knowledgebase' ) ) {
$archive_template = plugin_dir_path(__FILE__) . '/templates/archive-knowledgebase.php';
}
return $archive_template;
}
add_filter( 'archive_template', 'get_custom_post_type_template' ) ;
Try that, then let me know the result. Thanks

Wordpress redirect specific link to custom link

I have a wordpress website and I want to redirect a specific link to my custom link. I do not want to use any plugin and want to achieve this by writing code in functions.php file. The code that I tried is writen below
function custom_redirect() {
if(is_page(7)) {
wp_redirect('http://example.com/my-account/orders/', 301);
exit();
}
}
add_action ('template_redirect', 'custom_redirect');
Now the page, ( (is_page(7)) ), from which I want to redirect the users has the url which is http://www.example.com/my-account/ and I want to redirect them to http://www.example.com/my-account/orders. I tried the site_url('/my-account/') function also but unable to achieve that.
The problem is when you test for is_page('my-account') it will return true even when you are viewing an WooCommerce account endpoint. The workaround is to check the global $wp variable for the requested page slug.
function custom_redirect() {
global $wp;
if( $wp->request == 'my-account' ) {
wp_redirect( site_url( 'my-account/orders/' ) );
exit;
}
}
add_action ('template_redirect', 'custom_redirect');
i'm not sure that i understood, but i propose this:
function custom_redirect() {
if(is_page(7)) {
header('Location: http://example.com/my-account/orders/');
exit();
}
}
add_action ('template_redirect', 'custom_redirect');

Wordpress directly route to single Custom type result after searching

After searching (so ?s=foobar), I change the query :
function SearchFilter($query)
{
// If 's' request variable is set but empty
if(isset($_GET['s'])
&& empty($_GET['s'])
&& $query->is_main_query())
{
$query->is_search = true;
$query->is_home = false;
}
if ($query->is_search && !is_admin() ) {
$query->set('post_type',array('post','myFooBarCustomType'));
}
return $query;
}
But after the query, wordpress routes to the search.php page and I want to route to the single-customType.php page with the only-one result of my query. (a direct to the answer route with the good URL like www.mywebsite/myCustomType/foobar)
I only want the search.php page for a null result or more than 1.
Please help
Instead of your code, try adding this piece of code to your functions.php:
add_action('template_redirect', 'search_redirect_to_first_result');
function search_redirect_to_first_result() {
if (is_search()) {
if (have_posts()) {
the_post();
wp_redirect(get_permalink());
exit;
}
}
}

Prevent WordPress search from search a specific pages parentpages

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

Resources