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 ) );
Related
I am using a free Real Estate Wordpress plugin from the repository but the support is paid.
Anyway, the plugin's "City" taxonomy is NOT hierarchical. I need to make it HIERARCHICAL so I can create counties with hierarchical cities under it. As you know, modifying plugin's files is not a possibility for known reasons (update overwriting).
I am looking for something like this to deploy in functions.php:
function change_post_object_label( $taxonomy_args ) {
$taxonomy_args->hierarchical = true;
}
add_action( 'taxonomy_hook', 'change_taxonomy_args' );
Does it exist? How can I set hierarchical to "true" for a given taxonomy without having to alter the php files?
If you are working on WordPress 4.4 or a newer version, you may use the register_taxonomy_args filter.
Add this to your functions.php, and don't forget to use the actual taxonomy slug.
function filter_register_taxonomy_args( $args, $taxonomy ) {
if ( $taxonomy === 'taxonomy_slug_here' ) {
$args['hierarchical'] = true;
}
return $args;
}
add_filter( 'register_taxonomy_args', 'filter_register_taxonomy_args', 10, 2 );
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');
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
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' );
I have searched, here is the closes result.
I am building a new wordpress site. I want most posts to have no category in the URL, simply www.site.com/title. However I do want the blog posts to be separate, so I'd like www.site.com/blog/title. I'd also like the option to add more like that in the future, for only specific categories, not the entire site.
There are many questions similar to this here on stackoverflow but most have 0 replies. Any advice would be great. I've even tried Advanced Permalinks without any luck.
You can simply do that by Setting > Permalinks and add to Common Setting > Custom Structure the value /blog/%postname%/. There you will get the blog post accessible from www.site.com/blog/title.
I cannot understand the first question. By:
I want most posts to have no category in the URL
do you mean to NOT HAVING www.site.com/category/category-name? or not having www.site.com/category/post?
EDIT #1
To answer this:
www.site.com/category/post is what I ONLY want for blog posts with the category of "Blog" > if the category is "Shoes" I don't want the category displayed in the URL. –
First: you can set the Permalink to /%postname%/ so all your post will have site/title therefore accessible from that link
Second: You have to filter the permalink to behave differently for the posts under "Blog" category.
Try this
add_filter( 'post_link', 'custom_permalink', 10, 2 );
function custom_permalink( $permalink, $post ) {
// Get the categories for the post
$categories = wp_get_post_categories( $post->ID );
foreach ( $categories as $cat ) {
$post_cat = get_category( $cat );
$post_cats[] = $post_cat->slug;
}
// Check if the post have 'blog' category
// Assuming that your 'Blog' category slug is 'blog'
// Change 'blog' to match yours
if ( in_array( 'blog',$post_cats ) ) {
$permalink = trailingslashit( home_url( 'blog/' . $post->post_name ) );
}
return $permalink;
}
Third: You have to filter rewrite_rules
add_filter( 'rewrite_rules_array', 'custom_rewrite_rule' );
function custom_rewrite_rule( $rules ) {
$new_rules = array(
'blog/([^/]+)/trackback/?$' => 'index.php?name=$matches[1]&tb=1',
'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
'blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?name=$matches[1]&cpage=$matches[2]',
'blog/([^/]+)(/[0-9]+)?/?$' => 'index.php?name=$matches[1]&page=$matches[2]'
);
$rules = $new_rules + $rules;
return $rules;
}
Go to permalink setting and save the setting to refresh your rewrite rules and make the changes above active
NOTE: Add those functions on your active theme functions.php template
NOTICE: I haven't test it yet but that's the way you change permalink. I did the similar way to change my permalink on archives and search result.