WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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(
               'key' => 'dealer_country',
               'value' => $ajaxCounty,
               'compare' => '='
               '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){
            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){


$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
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

Your Answer


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.