0

I have a problem with a custom WP_Query. The query args are:

$args = array(
    'posts_per_page' => -1,
    'post_type'      => 'adressen',
    'orderby'        => 'title',
    'order'          => 'ASC',
    'meta_query'     => array(
        'relation' => 'AND',
        array(
            'key'     => 'adresse_anzeigen_%_anzeige_branche',
            'compare' => 'LIKE',
            'value'   => $clientId
        ),
        array(
            'key'     => 'adresse_anzeigen_%_anzeige_aktiv',
            'compare' => '=',
            'value'   => "1"
        )
    )
);

(The meta key pattern are predefined from Advanced Custom Fields)

My Problem in the dump of the WP_Query Object, WordPress converts it to this:

SELECT wp_2_posts.* FROM wp_2_posts
INNER JOIN wp_2_postmeta ON ( wp_2_posts.ID = wp_2_postmeta.post_id )
INNER JOIN wp_2_postmeta AS mt1 ON ( wp_2_posts.ID = mt1.post_id )
WHERE 1=1 AND
( 
    (
        wp_2_postmeta.meta_key = 'adresse_anzeigen_{783ec3ed63725295bd6fd75a91ca47bfebd15fa5b4aac1ab7eafd49f682034e6}_anzeige_branche' AND
        wp_2_postmeta.meta_value LIKE '{783ec3ed63725295bd6fd75a91ca47bfebd15fa5b4aac1ab7eafd49f682034e6}203{783ec3ed63725295bd6fd75a91ca47bfebd15fa5b4aac1ab7eafd49f682034e6}'
    )
    AND 
    (
        mt1.meta_key = 'adresse_anzeigen_{783ec3ed63725295bd6fd75a91ca47bfebd15fa5b4aac1ab7eafd49f682034e6}_anzeige_aktiv' AND
        mt1.meta_value = '1'
    )
)
AND wp_2_posts.post_type = 'adressen'
AND
(
    wp_2_posts.post_status = 'publish' OR
    wp_2_posts.post_status = 'acf-disabled' OR
    wp_2_posts.post_status = 'private'
)
GROUP BY wp_2_posts.ID
ORDER BY wp_2_posts.post_title ASC

The % sign and the quotes are not numbers? Could someone explain why?

2 Answers 2

1

As the dev note for 4.8.3 implies, you need to unescape the escaped % by using the $wpdb->remove_placeholder_escape method. Something like

global $wpdb;
add_filter('posts_request',array($wpdb, 'remove_placeholder_escape'));

Very untested.

1

This is normal behaviour since WP 4.8.3, where the % sign is encoded to prevent some forms of SQL injection. Here is the discussion about it, and other user reported the same problem: 4.8.3 prepare update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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