Support

Account

Home Forums Front-end Issues How get value from serialize array (after save checkbox)

Solved

How get value from serialize array (after save checkbox)

  • Hello,
    Using acf I have to check more checkboxes in the administration. Selected values are stored in the database as a serialized field. Can you please advise me how can I choose from database values? I have the following, but it does not work. Array $_POST[‘year’] contains value 15,16,17,18

    
    $args = array(
              'posts_per_page' => -1,
              'meta_query' => array(
              'relation' => 'AND',
        	   	array(
        			'key' => 'year',
        			'value' => $_POST['year'],
        			'type' => 'text',
        			'compare' => 'IN'
        		)));
    
  • on this page https://www.advancedcustomfields.com/resources/query-posts-custom-fields/ see 3. Multiple custom field values (array based values)

    but the value given in the examples in incorrect. You also need to include quotes around the value to find exact matches.

    
    'value' => '"Melborne"',
    'compare' => 'LIKE'
    
  • Thank you.The number of items $ _POST can change dynamically. So I have to create a cycle that will go through individual items?

  • You have to create a loop that goes over the value and dynamically generate the meta query. For example, assuming that your $_POST value is an array;

    
    $meta_query = array('relation' => 'OR');
    foreach ($_POST['year'] as $year) {
      $meta_query[] = array(
        'key' => 'year',
        'value' => '".$year."',
        'compare' => 'LIKE'
      )
    } // end foreach
    $args = array(
      'posts_per_page' => -1,
      'meta_query' => $meta_query
    );
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘How get value from serialize array (after save checkbox)’ is closed to new replies.