Changing search criteria
-
I have this custom search filter function https://pastebin.com/x1sbxwfS
but if a post has “Rif. 305” as a name (“name” is a custom field), i cant get it by putting just “305”, I have to put “Rif. 305” or “Rif. 30″ o r”Rif. 3”, how can I filter just putting the last carachters? (letters or numbers). Maybe have I to change this instruction?
'compare' => 'LIKE'-
This topic was modified 9 months, 1 week ago by
sacconi.
The page I need help with: [log in to see the link]
-
This topic was modified 9 months, 1 week ago by
-
Ciao sacconi,
I tested your code on my site. I added a custom field named “function_name” to a post, with a value of “Rif. 305”. I then made a generic search request like so:
https://my-test-site.com/?s=305
This worked as expected and the related post was found. Your problem lies elsewhere.I suspect the
$meta_query = array('relation' => 'OR');is not having the effect you think it should. This applies OR logic only to other meta queries, it does not apply OR logic to other search criteria such as taxonomies, titles, and what not. As you have no other meta queries, this line is ineffective, other than it establishes an array to push more data into.'relation' => 'OR'is harmless to exist, but it does nothing when only one meta value is specified.Your code results in a SQL query that in part is:
WHERE 1=1 AND ( ( wp_postmeta.meta_key = 'function_name' AND wp_postmeta.meta_value LIKE '%305%' ) ) AND ((wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish'All ANDs, no ORs! (there are other ORs in search queries, but not related to post meta) If you had other post meta criteria besides “function_name”, then those would be ORed, but only in relation to post meta, not other criteria.
You maybe want the first
ANDin the above SQL snippet to instead beOR? This is not easy to accomplish with WP_Query. You’d need to use the “posts’request” filter to search for the correctANDand replace only that instance withOR. Similar to “pre_get_posts”, all sorts of queries pass through “posts_request”. You must ensure you’re only altering the query that is supposed to be altered. For example by verifying there’s an “s” query var.I’m not convinced you want OR logic, but its lack would explain why searches are not working as you want. With OR logic, you tend to get more matches when multiple criteria is applied. With AND logic you get fewer or no matches. If you want matches that fit all the supplied criteria, use AND logic. For matches to only fit some of the criteria, then OR logic is called for. However, with OR logic you’ll more likely get poor quality matches. You might get a property whose function_name meta is Rif. 305, but none of the other criteria like 4 persons or 3 bedrooms may be present.
I use the search filter concerning “function_name” both in the public part of the site (there is a specific search box) and in the admin posts table, it’s a search completely independent from the main search box criteria, so no matter how many rooms or persons has the by-name-requested apartment. Probably I’m forced to accept to use only the initial words of the name. Normally apartments has a name with letters but sometimes local agencies classify their products in the following way: “rif. 001”, “rif 002” and so on, so I would type only the last part of this “name” (which is a number) because the initial part is the same for all the apartments of that agency. I’ll probably resign myself to just putting the number and deleting “rif.”, but in the future the admin will automatically generate contracts like my other site and it would be better if it printed the correct wording of the local agency.
-
This reply was modified 9 months, 1 week ago by
sacconi.
The SQL clause
wp_postmeta.meta_value LIKE '%305%' ) )should match any meta value that has “305” in it, no matter what else comes before or after. Should match “Rif 305” or “305” or “305 foo” or “Rif 305 foo”. The%in SQL is a wildcard character, much like*in many computer operating systems. Something else is amiss besides the code you linked to in your OP.It might have something to do with how your form sanitizes input maybe? Just guessing.
This other function can influence the previous one? https://pastebin.com/k4KB4tGJ
I noticed that the problem exists for numbers but not for letters
If I put the last three letters of a name using “function_name” as meta, it works, if I write “ano”, I get both “Milano” and “Stefano”, so with letters is working correctly.
About “search by ID” I have to introduce the exact ID with all the numbers, otherwise I cant get a match
Maybe can you help me also on this topic? https://ww.wp.xz.cn/support/topic/custom-taxonomies-boxes-in-the-central-edit-page-space/#post-18613708 – thank you
It’s quite possible the search by ID feature is overriding the post meta search functionality. It’ll try to find a post whose ID is 305 and could fail if such a post does not exist, even though there’s valid post meta with that value.
The ID search and function_name search use the same “s” search query var to modify default behavior, but their goals are contradictory. Ideally, the post ID search would utilize a separate form that does not leverage the “s” query var.
Do you use the search by ID functionality a lot? If not too frequent, you can get to the desired post from the editor screen of any post. The editor’s URL includes in part
post=1234where 1234 is the current post’s ID. You can alter this value to the desired ID, then go to the revised URL. The desired post will then appear in the editor.Searching by ID is important for both me and the customer. He may want to search for an apartment by its code (the post ID), and I also need to be able to quickly find apartments in the post table (admin).
-
This reply was modified 9 months, 1 week ago by
The topic ‘Changing search criteria’ is closed to new replies.