Modify the product search query - wordpress

I have replaced the wordpress search engine with the WooCommerce product search engine, that is, to include it in the header of my site I use:
get_product_search_form();
instead of :
get_search_form (true);
I would like the product search engine to also search the product categories.
The problem is that I have a category called "Jackets" but the product in its title and in its content uses the word without the "s" so that if I search for the word "jacket" I get many results, but if I search the word "jackets" (with "s") I do not get any, despite that being the name of the category.
I have thought about modifying the query that makes the request so that it adds something of the type: OR category_name LIKE $ search_term, and I tried to use pre_get_posts in functions.php to modify the query, but it doesn't work.
Could someone help me modify that search query for products so that I can also return the products that are of the category whose name matches with the search term.
Thank you very much in advance!
Kind regards,
Raquel

You can look at this link .
simple example:
// hook into wp pre_get_posts
add_action('pre_get_posts', 'jc_woo_search_pre_get_posts');
function jc_woo_search_pre_get_posts($q){
if ( is_search() ) {
add_filter( 'posts_join', 'jc_search_post_join' );
add_filter( 'posts_where', 'jc_search_post_excerpt' );
}
}

Related

In wordpress where is the code that differentiates between categories and standard pages?

This is a question for Wordpress experts:
Where, in the Wordpress machine, is the code that decides whether to route a query to the standard page or to a category? Does it try to match specific strings in the URL looking for categories and then, if it doesn't get a match, default to the code that looks for the page in the wp_posts table?
This is something I have been wondering about for a while but I have found it difficult trying to trace the path of the query through the system.
Thanks for any insights you can give!
There is a function for this you can use to hook in a page or a category.
function is_category( $category = '' ) {
}
function is_page( $page = '' ) {
}
$category / $page can be ID, name, slug or array

Using cpt and pass a variable using url

I want to create a nice readable permalink structure for my custom post type (CPT). My CPT "movie" has the following rewrite-slug movie/movie_name" (all works fine).
Now i want to add arg like this: movie/movie_name/arg and use arg in my template file as a php variable.
But obvious it lead to not-found-page. How can i achieve this target?
edit: i want it in FRIENDLY URL format, it means i dont want to use GET for this.
You may pass it like movie/movie_name?movie_arg=movie_value. It is will be available with $_GET['movie_arg']. Of course your need extra sanitization to handle this data.
To be able to read this in a WordPress way add params to a query_vars filter
function add_movie_arg_to_query_vars( $qvars ) {
$qvars[] = 'movie_arg';
return $qvars;
}
add_filter( 'query_vars', 'add_movie_arg_to_query_vars' );
Note: it should not be same as reserved WordPress query parameters
This way it will be available at your template with get_query_var('movie_arg')
print_r( get_query_var('movie_arg') ) // movie_value
More information here

How to retrieve the latest categories assigned to a post currently under editing?

I need to limit the post tile length of a post belonging to a specific category while editing. So I need to check what categories have been assigned to the post under editing and decide whether to limit or not its post title.
I use "wp_insert_post_data" to do the job
add_filter( 'wp_insert_post_data' , 'limit_title_length' , '99', 2 );
But what I found is that the categories returned from passed $postarr are existing categories. Not the latest categories. So it would not work for new post or if categories being changed while editing.
$post_category = $postarr['post_category'];
I also checked get_the_category() inside the function, and that also returns existing categories, not the latest categories if category assignment changed.
My codes so far...
function limit_title_length( $data, $postarr ) {
// set up variables like max length, category id to limit
...
// get post id, title and categories from $data and $postarr passed
$title = $data['post_title'];
$id = $postarr['ID'];
$post_category = $postarr['post_category'];
// check if the specified category exists in the categories assigned to this post
...
// process $title, reset $post_title in $data
...
$data['post_title'] = $title;
return $data;
}
add_filter( 'wp_insert_post_data' , 'limit_title_length' , '99', 2 );
wp_insert_post_data fires in the very late stage of post publishing. I expected to get the latest categories from $postarr['post_category'];
but it's not in my case here. Any solutions or alternatives?
So just to be clear - You want to identify the most recent category added to a post?
If this is the case you will be hard pressed to do so as Wordpress does not save meta for added categories. If you are skilled enough you could script such a function and save the data in a custom table. Then retrieve it for use.
If you know the name of the category you are looking for you can use has_category! .

Exclude comments of posts in certain categories in WordPress RSS feeds

So I can exclude posts in a certain category from showing in a WordPress RSS (etc) feeds, my problem is that I also need to stop comments to those posts appearing.
But going to http://www.example.com/comments/feed/ shows the comments to those posts, which should remain private.
Searched high and low, tried all sorts, so hoping some experts on here can help!
function custom_comment_feed_where($where) {
global $wpdb;
// get the post type
$where .= " AND $wpdb->posts.post_type NOT IN (
'xxx',
'yyy'
)";
return $where;
}
add_filter('comment_feed_where', 'custom_comment_feed_where');

How can I add custom meta fields in categories?

Does anyone have any idea how to add custom meta fields while making categories and fetch them in the loop in WordPress? I was wondering how to do that without hacking the WordPress core, but if I do – it won't become a hindrance to update WordPress in the future.
A plugin I have found that comes close is Wp-Category-Meta, but it doesn't have the ability to add checkboxes as fields in Edit Categories.
This will be very useful as users can make certain categories "featured", and then the code can use that meta value in the loop to style "featured" categories differently.
The problem:
Wordpress does not have a structure nor method to store "meta" values for taxonomies.
UPDATE 2017: WP 4.4+ has "term meta"!
For working with term metas use these:
update_term_meta()
get_term_meta()
delete_term_meta()
add_term_meta()
The Actions below are still valid though! :)
Additional reading: 4.4 Taxonomy Roundup
Solution for WP version <= 4.3.x and COMMON actions
Actions:
create_category and edit_category for category edit
category_add_form_fields and category_edit_form for category form fields
There are more actions than I've presented, but they seem to be deprecated (according to developer.wordpress.org).
The reason I chose the actions that I chose:
- They work on WordPress 4.4.2
- Due to lack of documentation I assumed these are the new ones replacing the deprecated ones...
Functions:
get_option( $option, $default );
update_option( $option, $new_value, $autoload );
update_option has two great abilities:
a) It craetes the option when such option does not exist yet
Unless you need to specify the optional arguments of add_option(),
update_option() is a useful catch-all for both adding and updating
options.
b) $new_value can be an integer, string, array, or object.
You may ask, why to use array/object? ...well, because each option = 1 database row => you probably want to store your category options in one row :)
The CODE
function my_category_form_fields($tag_object){
//output/display extra form fields, e.g. by echo ...
//ADD EXTRA SPECIFIC FIELD TO LATER CHECK IF IT'S CATEGORY SAVE/EDIT!
//(see note at 'edit_category' action...)
if( !empty($tag_object['term_id']) ){
//edit category form specific
//...load existing options with get_option( $option, $default );
} else {
//create category form specific
}
}
function my_category_save(){
//CHECK FOR YOUR EXTRA SPECIFIC FIELD TO CHECK IF IT'S CATEGORY SAVE/EDIT
//(see note at 'edit_category' action...)
//SECURITY CHECK
if( empty($_POST['EXTRA_SPECIFIC_FIELD']) || ! current_user_can('manage_categories') )
return null;
//save your form values using update_option()
//Recommendation:
//Add "category_" prefix and $category_id to your option name!
}
add_action( 'create_category', 'my_category_save', 10, 1 );
//Runs when a category is updated/edited,
//INCLUDING when a post or blogroll link is added/deleted or its categories are updated
//(which causes the count for the category to update)
add_action( 'edit_category', 'my_category_save', 10, 1 );
add_action( 'category_add_form_fields', 'my_category_form_fields', 10, 1 );
add_action( 'category_edit_form', 'my_category_form_fields', 10, 1 );
Create or Edit?
You might wonder whether you are creating or saving a category - this not documented yet (as far as I know), but from testing:
Edit save => $tag_object is object and contains some properties, most notably:
term_id
taxonomy
filter
Create save => $tag_object is just a regular string "category" - I guess this might change in the future...
General taxonomy
There are also actions like these for taxonomies in general - check these actions.
Jaz,
It looks like the plugin you mention in your original question has been updated to include a checkbox field (included in v1.2.3)
I think the Category SEO Meta Tags plugin will help you.
There is an updated and refactured version of this plugin to be found here:
https://wordpress.org/plugins/custom-taxonomy-category-and-term-fields/
Also added a WYSIWYG editor fieldtype.

Resources