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 WP Query and I am simply comparing by the meta value by =

My value contains town names, and seems to work great for the majority of names.

However some names contain apostrophes... Madonna dell'olmo

And the meta values with apostrophes fail to work.


This is my query...

$dealerResults = new WP_Query(array(
    'posts_per_page' => -1,
    'post_type'     => 'dealer',
    'meta_query'    => array(
        array(
               'key' => 'dealer_country',
               'value' => $ajaxCounty,
               'compare' => '='
        ),
        array(
               'key' => 'dealer_location',
               'value' => $ajaxTown,
               'compare' => '='
        )
    )      
));


I am passing the from a option value through some jquery function and ajax.

foreach($value as $town){
    echo "<option class=\"$county_class\" value=\"$town\">$town</option>";
}

-

dealerResults = function () {
    var county = $('#dealer-county').val(),
        town = $('#dealer-town').val(),
        wp_nonce = '<?php echo wp_create_nonce("dealer_search");?>';
    if(county != 0 && town != 0){
        $.ajax({
            type: 'POST',
            url: '<?php echo admin_url('admin-ajax.php'); ?>?r='+Math.random(),
            data: { varCounty : county, varTown : town, action: 'dealer_search', nonce: wp_nonce },
            success: function(data){
                $(".dealer-wrapper").remove();
                $("#dealer-results").append(data);      
            }
        });
    }
}

-

$ajaxCounty = $_POST['varCounty'];
$ajaxTown = $_POST['varTown'];



My question is how can I get the query to work for my meta values which contain apostrophes?


Thanks in advance

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

WordPress adds slashes to the REQUEST/GET/POST superglobals, if I'm not mistaken. Try this change:

$ajaxCounty = stripslashes( $_POST['varCounty'] );
$ajaxTown = stripslashes( $_POST['varTown'] );

Related Trac ticket.

share|improve this answer
    
Duuuude.. you rock! Thank you for your help. Instant fix. –  Joshc Feb 28 '13 at 17:42
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.