Support

Account

Home Forums General Issues Querying the database for serialized repeater sub field values

Solved

Querying the database for serialized repeater sub field values

  • I have a repeater field: contributing_game_creators
    I have some nested checkboxes: game_creator_roles

    Values for game_creator_roles are getting stored in the database like this: a:2:{i:0;s:7:”Concept”;i:1;s:6:”Design”;}

    Following this documentation page, I realise that I need to use a wpdb SELECT statement for values matching ‘contributing_game_creators_%_game_creator_roles’ which has a wildcard value in it for matching all possible rows.

    Using the following query I’m unable to get any posts.

    $games_id_array = $wpdb->get_results($wpdb->prepare( 
    "
    SELECT * 
    FROM wppp_postmeta
    WHERE meta_key LIKE %s
    AND meta_value = %s
    ",
    'contributing_game_creators_%_game_creator_roles',
    'Concept'
    //'a:2:{i:0;s:7:"Concept";i:1;s:6:"Design";}'
    ));

    Is it possible to query the database with a wpdb SELECT statement for values contained in a serialized array?

  • Hi @Bassscape

    Your above query will only find posts (that is if the query works as expected) that have Concept and Design selected as the value.

    To allow for a value within the array, you shoudl take a look at the checkbox docs:
    http://www.advancedcustomfields.com/resources/field-types/checkbox/

    On that page, you will see how to use a LIKE statment in the meta_query to match a value.

    I have not yet combined this with a sub field DB search, but it must be possible with SQL.

    Good luck mate

    Cheers
    E

  • Thanks for your input Elliot, it led me on to find my solution.

    I realised I wasn’t using the LIKE operator for my meta_value query and therefore the wildcard values weren’t be recognised. I changed my query to the following and it now returns posts:

    $games_id_array = $wpdb->get_results(
    	$wpdb->prepare( 
    		"
    			SELECT * 
    			FROM wppp_postmeta
    			WHERE meta_key LIKE %s
    			AND meta_value LIKE %s
    		",
    		'contributing_game_creators_%_game_creator_roles',
    		'%'.$search_value.'%'
    	)
    );
  • Hi @Bassscape

    Great work o the query!

    Cheers
    E

  • Hi @Elliot Condon, I could not understand why you people preferred using serialized field value for storing multiple values while WordPress support multiple meta values against single meta key for an object. Why you have not used that?
    If you had used that, it would be much easier for creating a query on a meta value.
    I hope you got my point. Please consider this in your next update, thanks.

    Regards,
    Aatif Farooq
    Sr. Web Developer
    A WordPress Ninja

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Querying the database for serialized repeater sub field values’ is closed to new replies.