Home Forums Add-ons Flexible Content Field Post order by Flexible Content custom field


Post order by Flexible Content custom field

  • How do you order a custom post type by Flexible Content custom field?

    I have this code, but it doesn’t work with a Flexible Content custom field:

    function my_pre_get_posts( $query ) {
    	// do not modify queries in the admin
    	if( is_admin() ) {
    		return $query;
    	// only modify queries for 'event' post type
    	if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'event' ) {
    		$query->set('orderby', 'meta_value');	
    		$query->set('meta_key', 'start_date');	 
    		$query->set('order', 'ASC'); 
    	// return
    	return $query;
    add_action('pre_get_posts', 'my_pre_get_posts');
  • There isn’t any way within WP to order posts by a flex or repeater sub field.

    The sub fields of these fields are stored using a meta key with that is constructed like this: "{$parent_name}_{$index}_{$sub_field_name}"

    In order to do this you could possibly query the DB directly using $wbdp

    Some parts of the query might look something like this. This is not tested.

    SELECT post_id, wp_postmeta.meta_key LIKE "{$parent_name}_{$index}_{$sub_field_name}" AS date ORDERBY date ASC

    You could then use the list of returned post ID value and use “post_in” and ‘order_by’ => ‘post__in’

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

You must be logged in to reply to this topic.