Support

Account

Home Forums Add-ons Repeater Field Show post/product if current date matches a date picker value in a subfield

Solved

Show post/product if current date matches a date picker value in a subfield

  • Hi everybody,

    I’m trying to show a post (actually, a woocommerce product, hence a custom post type) if one of the date values in a subfield of a repeater field matches the current date.

    I’ve structured my query as per here, but seem to get no luck in the end.

    The fields (formatted in bold):
    pianificazioni = a repeater field to add dates to a product display schedule;
    when_j1d = a subfield (of the datepicker type) to select dates as per above; may include dates before and after the current date.

    The code

    
    //filter (in functions.php):
    function my_posts_where( $where ) {
    	
    	$where = str_replace("meta_key = 'pianificazioni_%", "meta_key LIKE 'pianificazioni_%", $where);
    
    	return $where;
    }
    add_filter('posts_where', 'my_posts_where');
    
    //code in the template file: getting today's date
    	$date = date('Ymd');
    
    //args
    
    $args = array(
    	'post_type'	=> 'product',  
    	'meta_query'	=> array(
    		'relation'	=>	'AND',
    		array(
    			'key'		=> 'pianificazioni_%_when_j1d',
    			'compare'	=> '=',
    			'value'		=> $date
    			)
    		) 		
    	);
    

    It seems not to work; any ideas?

    Any help would be very appreciated!

    Thanks,

    Alessio

  • Solved!

    Given the filter added in the functions.php file, I had to edit

    'compare' => '='

    to

    'compare' => 'LIKE'

    it now works perfectly! Hope this helps others.

    Alessio

  • For anyone else who comes across this solution when trying to search for repeater sub-fields:

    Note that due to changes in WordPress, you can no longer use the percent sign as your wildcard. The percent sign will be substituted with a hash and your query will come up empty. So, see http://www.advancedcustomfields.com/resources/query-posts-custom-fields/ for a related example, which suggests using the dollar sign instead.

    Since the dollar sign is used to indicate variable names in php, though, and can therefore cause confusion if you’re using a text editor with syntax highlighting, I prefer to use a string like XYZ for the substitution instead.

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

The topic ‘Show post/product if current date matches a date picker value in a subfield’ is closed to new replies.