Custom field filters
- PiteusParticipant3 years, 10 months ago #26257
Hello, i’m trying to use custom fields as filters, so in Filters I select “Custom_data” like product_cat, that I already use, but it doesn’t appears in the filters.
Another question, “custom_data” is a Date so can i use a filter like “between Date1 and Date2”?wpsolrKeymaster3 years, 10 months ago #26258Your custom field must have been selected in tab 2.2, with the data type “Date”, then your data reindexed. It should then appear in your WPSOLR search filters widget.
You can show your filter with a date slider layout.PiteusParticipant3 years, 10 months ago #26296In my custom field date is “dd/mm/yy” Ex. 4 January 2021 -> 04/01/2021, but plugin index it as “1 April 2021”.
But still using US format when I use range slider does not find any products, this is the query in nav bar:
?element=%5Bobject%20Object%5D&wpsolr_fq%5B0%5D=custom_data_str%3A1585103563636-1612137600000
1585103563636 -> Wednesday 25 March 2020 02:32:43.636
1612137600000 -> Monday 1 February 2021 00:00:00
But i’ve a product with date 07/23/2020(which once solved I will mark as 23/07/2020)wpsolrKeymaster3 years, 10 months ago #26297Indeed, WPSOLR indexes dates from strtotime(), which seems to take a US date format by default.
I do not know a good way to convert a date from any format (like Italian) to US before calling strtotime().Even more problematic, what if custom dates are stored in several formats (Italian, US)?
Or if WPML is used and all dates must be stored according to the languages?PiteusParticipant3 years, 10 months ago #26298If you use strtotime() should be enough to replace “/” with “-“(25/05/2020 -> 25-05-2020)
https://stackoverflow.com/questions/2891937/strtotime-doesnt-work-with-dd-mm-yyyy-format
Apparently there are two possible formats European or US, if it’s a problem for you, you can tell me where edit code to replace the symbols without having to generalize it in your pluginwpsolrKeymaster3 years, 10 months ago #26299Unfortunately, this still requires WPSOLR knowing the current custom field date format is European or US.
But here is your workaround to solve your issue:
<?php use wpsolr\core\classes\WPSOLR_Events; add_filter( WPSOLR_Events::WPSOLR_FILTER_POST_CUSTOM_FIELDS, 'filter_custom_fields', 10, 2 ); /** * @href https://stackoverflow.com/questions/2891937/strtotime-doesnt-work-with-dd-mm-yyyy-format * * Convert a European formatted custom field date to US formatted date, so strtotime() yields the correct value for Elasticsearch Date field * 25/01/2021 => 25-01-2021 * * @param array $custom_fields * @param int $post_id * * @return mixed */ function filter_custom_fields( $custom_fields, $post_id ) { $custom_fields['custom_field_date_ddmmyy'] = str_replace( '/', '-', $custom_fields['custom_field_date_ddmmyy'] ); return $custom_fields; }
PiteusParticipant3 years, 10 months ago #26300Thanks, it works perfectly!
But when i’m using date slider it doesn’t match any items, in the bar I can see first and last value that are correct but if i try to move it, for example i’ve selected from 10 August 2020 to 2 January 2021, it show me “No products were found matching your selection.” but i’ve more products in this range for example 1 December 2020
Do you know why it happens?PiteusParticipant3 years, 10 months ago #26302Query:
[ { "query": "*", "offset": 0, "length": 10, "maxValuesPerFacet": 10, "sortFacetValuesBy": "alpha", "attributesToRetrieve": [ "id", "PID", "type", "meta_type_s", "title", "numcomments", "comments", "displaydate", "displaymodified", "*categories_str", "author", "*post_thumbnail_href_str", "*post_href_str", "snippet_s" ], "filters": "(type:\"product\") AND (NOT post_status_s:\"draft\" AND NOT post_status_s:\"pending\" AND NOT post_status_s:\"trash\" AND NOT post_status_s:\"future\" AND NOT post_status_s:\"private\" AND NOT post_status_s:\"auto-draft\") AND (NOT product_visibility_str:\"exclude-from-search\") AND ((NOT is_excluded_s:*) OR (is_excluded_s:\"n\"))AND ((custom_data_dt_i:1594951816137 TO 1609545600000))", "facets": [ "custom_data_dt_i", "flat_hierarchy_product_cat_str", "product_tag_str", "type", "post_status_s", "product_visibility_str", "is_excluded_s" ], "indexName": "dev_piteus_replica_sort_custom_data_dt_i_asc" }, { "facets": [ "filterOnly(type)", "filterOnly(post_status_s)", "filterOnly(is_excluded_s)", "custom_data_dt" ], "hitsPerPage": 0, "page": 0, "attributesToRetrieve": [], "attributesToHighlight": [], "attributesToSnippet": [], "analytics": false, "clickAnalytics": false, "filters": "(type:\"product\") AND (NOT post_status_s:\"draft\" AND NOT post_status_s:\"pending\" AND NOT post_status_s:\"trash\" AND NOT post_status_s:\"future\" AND NOT post_status_s:\"private\" AND NOT post_status_s:\"auto-draft\") AND ((NOT is_excluded_s:*) OR (is_excluded_s:\"n\"))", "indexName": "dev_piteus_replica_sort_custom_data_dt_i_asc" } ]
Result:
[ { "hits": [], "nbHits": 0, "offset": 0, "length": 10, "facets": [], "exhaustiveFacetsCount": true, "exhaustiveNbHits": true, "query": "*", "params": "query=*&offset=0&length=10&maxValuesPerFacet=10&sortFacetValuesBy=alpha&attributesToRetrieve=%5B%22id%22%2C%22PID%22%2C%22type%22%2C%22meta_type_s%22%2C%22title%22%2C%22numcomments%22%2C%22comments%22%2C%22displaydate%22%2C%22displaymodified%22%2C%22*categories_str%22%2C%22author%22%2C%22*post_thumbnail_href_str%22%2C%22*post_href_str%22%2C%22snippet_s%22%5D&filters=%28type%3A%22product%22%29+AND+%28NOT+post_status_s%3A%22draft%22+AND+NOT+post_status_s%3A%22pending%22+AND+NOT+post_status_s%3A%22trash%22+AND+NOT+post_status_s%3A%22future%22+AND+NOT+post_status_s%3A%22private%22+AND+NOT+post_status_s%3A%22auto-draft%22%29+AND+%28NOT+product_visibility_str%3A%22exclude-from-search%22%29+AND+%28%28NOT+is_excluded_s%3A*%29+OR+%28is_excluded_s%3A%22n%22%29%29AND+%28%28custom_data_dt_i%3A1594951816137+TO+1609545600000%29%29&facets=%5B%22custom_data_dt_i%22%2C%22flat_hierarchy_product_cat_str%22%2C%22product_tag_str%22%2C%22type%22%2C%22post_status_s%22%2C%22product_visibility_str%22%2C%22is_excluded_s%22%5D", "index": "dev_piteus_replica_sort_custom_data_dt_i_asc", "processingTimeMS": 1 }, { "hits": [], "nbHits": 8, "page": 0, "nbPages": 0, "hitsPerPage": 0, "facets": [], "exhaustiveFacetsCount": true, "exhaustiveNbHits": true, "query": "", "params": "facets=%5B%22filterOnly%28type%29%22%2C%22filterOnly%28post_status_s%29%22%2C%22filterOnly%28is_excluded_s%29%22%2C%22custom_data_dt%22%5D&hitsPerPage=0&page=0&attributesToRetrieve=%5B%5D&attributesToHighlight=%5B%5D&attributesToSnippet=%5B%5D&analytics=false&clickAnalytics=false&filters=%28type%3A%22product%22%29+AND+%28NOT+post_status_s%3A%22draft%22+AND+NOT+post_status_s%3A%22pending%22+AND+NOT+post_status_s%3A%22trash%22+AND+NOT+post_status_s%3A%22future%22+AND+NOT+post_status_s%3A%22private%22+AND+NOT+post_status_s%3A%22auto-draft%22%29+AND+%28%28NOT+is_excluded_s%3A*%29+OR+%28is_excluded_s%3A%22n%22%29%29", "index": "dev_piteus_replica_sort_custom_data_dt_i_asc", "processingTimeMS": 1 } ]
Did you mean this?
wpsolrKeymaster3 years, 10 months ago #26306For instance, start with:
{ "filters": "(type:\"product\") AND (NOT post_status_s:\"draft\" AND NOT post_status_s:\"pending\" AND NOT post_status_s:\"trash\" AND NOT post_status_s:\"future\" AND NOT post_status_s:\"private\" AND NOT post_status_s:\"auto-draft\") AND (NOT product_visibility_str:\"exclude-from-search\") AND ((NOT is_excluded_s:*) OR (is_excluded_s:\"n\")) AND ((custom_data_dt_i:1594951816137 TO 1609545600000))"}
Screenshot of the Algolia “Add Query Parameters”:
You must be logged in to reply to this topic.