Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this custom query that Im working on and I need to be able to query output values as well as actual stored values.

Example:

$string = "This is #*&^% a test";
$string = preg_replace('/[^(\x20-\x7F)]*/','', $string);
$string_clean = $string

So if I echo $string_clean, I'll get this:

This is a test

Which is fine, but the issue is that the $string_clean value isn't a stored value (just an output), so I dont know how to use its value in a query, being that the query results are based on stored database values.

Im using a multi array capability of the wordpress query (here). Basically, I use two keys in the array with AND as the meta_compare.

Ideally, I would like to be able to query both value (stored and output). Im interested in this as a search functionality for my query.

Can someone help me with this? How do I do it?. Or if someone knows of a better solution please let me know.

UPDATE:

This is the code that Im working with. I couldn't find any reliable solutions to implement REGEXP. Basically, what this should do, in theory, is use the php preg_replace function to remove/replace any special characters in the string and query the new clean string, that will then be used in the query's argument. Here it is:

*The initial value of the variable are submitted through a simple html form (not included).

if(isset($_POST['providername'])) {

    $providername = $_POST['providername'];

    if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $providername))
        {
            $clean = $providername; 

            // Replace other special chars
            $specialCharacters = array(
            ',' => '',
            '&amp' => 'and',
            '@' => 'at',
            '.' => '',
            '+' => '',
            '=' => '',
            '/' => '',
            '*' => '',
            '!' => '',
            '(' => '',
            ')' => '',
            '$' => '',
            '#' => '',
            '%' => '',
            '\'' => '',
            '_' => '',
            '^' => '',
            '-' => ''
            );

            while (list($character, $replacement) = each($specialCharacters)) {
            $clean = str_replace($character, '-' . $replacement . '-', $clean);
            }

            // Remove all remaining other unknown characters
            $clean = preg_replace('/[^a-zA-Z0-9\-]/', ' ', $clean);
            $clean = preg_replace('/^[\-]+/', '', $clean);
            $clean = preg_replace('/[\-]+$/', '', $clean);
            $clean = preg_replace('/[\-]{2,}/', '', $clean);

            if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $providername)) {
                $providername = $clean;
            } else {
                $providername = $_POST['providername'];
            }
        }
}

if(isset($_POST['providerloc'])) {
    $provider_location = $_POST['providerloc'];
}

This is the multi array argument:

$args = array(
    'count_total' => true,
    'role' => 'subscriber',
    'offset' => 8,
    'number' => 8,
    'meta_query' => array(
    'relation' => 'AND',
        array(
            'key' => 'jabber',
            'value' => $provider_location,
            'compare' => 'LIKE'
        ) ,
        array(
            'key' => 'keywordcontent',
            'value' => $providername,
            'compare' => 'LIKE'
        )
      )
);

After testing this solution, I get no results when trying to search for the "clean" outputted value.

For example:

If...

$value = "G-P's Workin' Barn";

After applying preg_replace, the output will be...

GPs Workin Barn

Now...if I search for "GPs Workin Barn" (the output value), I get no results. But when I search for "G-P's Workin' Barn" (the original value), it shows the result for that term. So it seems that the query isn't able to process the output value. As I thought, this makes sense because the query is searching against stored values in the database...There should be a way for the query to interpret/translate the output value and compare it against the stored value. After searching online, the closest solution that relates to what I need is the post_where filter for posts [credit @Rarst]. There doesn't seem to be a similar filter for users. Even so, I need a way to search using REGEXP against actual variable values.

Any thoughts or solutions? Thanks

share|improve this question
1  
I don't have an answer offhand to your question, but it occurs to me that with a minor core hack to allow the WP_Query object to accept "REGEXP" as an operator, you could probably do just that with the existing API. You may want to weigh in on this ticket with your use case and see if you can stimulate some discussion. –  goldenapples Nov 9 '11 at 21:52
    
@ goldenapples, thanks for the feedback. I'll look into it and see what I can find. –  user1893 Nov 9 '11 at 22:29
    
Im searching for some solutions, in the mean time where can I edit the WP_Query in wordpress? –  user1893 Nov 10 '11 at 20:55
    
I would go with the filter on posts_where that @Rarst suggested, actually, rather than hacking core. Take the "clean" value you want, explode it on '', and implode with an arbitrary number of non-alphanumeric characters (ie '[^a-zA-Z0-9\-]*') in between each character. Then use the "REGEXP" operator in your posts_where clause. –  goldenapples Nov 12 '11 at 17:35
    
I see, how do I use the REGEXP operator? And more importanly, how do I use the posts_where filter to get a list of users. The wp_user_query doesnt have that filter, but it does have a $query_where. Is the posts_where and $query_where the same? I updated my post with the example Ive been working on. Can you tell me how to include the REGEXP to it or how to implement the Wp_user_query to include the $query_where and the REGEXP operator? –  user1893 Nov 13 '11 at 16:38
show 2 more comments

1 Answer 1

You can try to make use of filters in query (likely posts_where) to make it look for match between clean value and MySQL REGEXP command (if it fits, didn't use it myself) with your logic, run on raw value.

share|improve this answer
    
Thanks Rarst. I need filters for user queries. I searched and there aren't that many. –  user1893 Nov 10 '11 at 20:53
    
@user1893 hmmm... User query as in WP_User_Query? From quick look there is pre_user_query that passes object by reference and should get you access to parts of query (in object's properties) before query runs. –  Rarst Nov 10 '11 at 21:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.