Support

Account

Home Forums Add-ons Repeater Field searching repeater sub field value with WP_User_query

Solved

searching repeater sub field value with WP_User_query

    • felicoz

    • December 24, 2018 at 6:59 pm

    hello in my project I have a coupon list (repeater) for each registered user, the coupon comes with expiry date and coupon amount that can be used.

    I came across this tutorial under “4. Sub custom field values” that I can search for repeater fields with WP_query using the meta_key but it doesn’t seem to be working with WP_user_query. Does anyone have experience with it?

    https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    thanks,
    Felix

    • felicoz

    • January 3, 2019 at 2:00 pm

    updated, found the solution!

    there is a similar “post_where” filter for WP_User_Query, that you can filter out the repeater sub field values.

    
    function user_meta_callback( $user_query )
    {
    	global $wpdb;
    	$user_query->query_where = str_replace("meta_key = 'coupons_$", "meta_key LIKE 'coupons_%", $user_query->query_where);
    }
    add_filter('pre_user_query', 'user_meta_callback');
    	
    $q = new WP_User_Query(array(
    	'numberposts'	=> -1,
    	'meta_query'	=> 
    		array(
    			'relation'		=> 'OR',
    			array(
    				'key'		=> 'coupons_$_date',
    				'compare'	=> '>=',
    				'value'		=> '2018-11-01',
    				'type'		=> 'DATE'
    			)
    		)
    	));
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.