Support

Account

Home Forums Add-ons Repeater Field Order custom post by sub custom field values

Helping

Order custom post by sub custom field values

  • Hi,

    I want to order my product according the sub custom field value (repeater).

    This is part of my code :

    
    $args = array(
        		'post_type' => 'produit',
        		'meta_query' => 
        		array('relation' => 'AND',
        			array(
                    'key' => 'fabricant',
                    'value' => $marque_id,
                    'compare' => 'LIKE'
                    ),
        		'tax_query' =>
        		array(
        			array(
        				'taxonomy' => $term,
        				'field' => 'id',
                    'terms' => $cat_id, 
                    'include_children' => true,
                    )
        			),
            'meta_key' => 'offres_fournisseurs_%_prix',
            'orderby' => 'meta_value meta_value_num',
            'order' => ASC,
            'posts_per_page' => -1,
            );
        // The Query
        	$the_query = new WP_Query($args);
    

    I want to order my product according the repeater sub value, is that possible or i need to use $wpdb->get_results to sort correctly my product ?

    My products are randomly order, i think wordpress take only the first occurence of my repeater and sort elements with

  • Hi @PrIInT

    Thanks for the question.

    Just to clarify this concept. It is not possible to sort posts by a custom field value, where each custom field value has multiple values (multiple rows of data in a repeater field).

    This said, it IS possible to sort the data if you only want to use the ‘first’ row from the repeater field. This means that WP has one meta_value per post and can then sort as per usual!

    at the moment, your orderby meta_key query looks like this:

    
    'offres_fournisseurs_%_prix'
    

    All you need to do is construct the exact meta_key which exists in the database. You are extremely close, but need to cahnge the wildcard (%) to 0 to target the first row:

    
    'offres_fournisseurs_0_prix'
    

    Hope htat helps.

    Thanks
    E

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

The topic ‘Order custom post by sub custom field values’ is closed to new replies.