Support

Account

Home Forums General Issues Ordering by Custom Field That Stores JSON

Solved

Ordering by Custom Field That Stores JSON

  • I have a field that stores a JSON string from YouTube. I wish to create a custom query that would sort by a value within the JSON. In a perfect world, a meta_key like this would work, but it obviously does not. Is there a way to do this?

    
    $args = array( 
      'posts_per_page' => -1,
      'post_type' 	   => 'videos',
      // video_json_string is the ACF field name
      'meta_key'	   => 'video_json_string['items'][0]['statistics']['viewCount']', 
      'orderby'	   => 'meta_value'
    );
    $query = new WP_Query($args);
    

    I know I could create a new field and store the view count there by itself, but I want to see if it can be done in this fashion. Thank you for any help!

  • If it’s an ACF field it does not store JSON. ACF fields that contain arrays store serialized values.

    There isn’t WP_Query() on fields like these and the best option is what you suggest, store the value you want to search and filter on into it’s own meta_key.

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

You must be logged in to reply to this topic.