• I’m having trouble matching a data that is a serialized array.
    I had assumed that the following code would work but it doesn’t. I only want to return records for which the key is defined and the serialized data contains the key value pair where the value is true.

    $args = array(
    	'post_type' => 'page',
    	'orderby' => 'title',
    	'order' => 'ASC',
    	'meta_query' => array ( array ('key' => '_fbfp', 'value' => '%isfanpage%true%', 'compare' => 'LIKE' ) )
    	);
    $posts = new WP_Query($args);
Viewing 2 replies - 1 through 2 (of 2 total)
  • The problem is that WP escapes the % sign in the ‘value’ => ‘%isfanpage%true%’ clause and adds its own % signs, so what is sent to MySQL is this:

    ‘%\\%isfanpage\\%true\\%%’

    I think one way to solve this would be to use ‘value’ => ‘isfanpage%true’ and then use a filter to replace ‘\\%’ with ‘%’ in the query.

    This still may produce unexpected results if the word true occurs anywhere after the word isfanpage, not just as its value. It might be better to use the proper number of underscores between the two words and replace ‘\\_’ with ‘_’.

    Thread Starter AFriedl

    (@afriedl)

    Thanks vtxyzzy, I had to query based on the existence of the key and then cycle through the results in PHP. There doesn’t seem to be a good way to do this.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘meat_query and serialized data’ is closed to new replies.