WordPress.org

Ready to get started?Download WordPress

Forums

meat_query and serialized data (3 posts)

  1. AFriedl
    Member
    Posted 2 years ago #

    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);
  2. vtxyzzy
    Member
    Posted 2 years ago #

    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 '_'.

  3. AFriedl
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic