Support

Account

Home Forums Add-ons Repeater Field Relationship field within repeater returning empty array

Solved

Relationship field within repeater returning empty array

  • Hi,

    I have two CPTs (Events, Drivers) and ‘Events’ has a custom taxonomy of ‘Seasons’.

    In my Events CPT I have a repeater for ‘driverresults’ with a relationship sub-field of ‘driver’.

    I’m trying to query all events, within a specific season, and also add a meta query to specify the driver.

    My query is as follows:

    <?php
    					/* Loop through events in this season and find any results fields where this driver is listed */
    					$args = array(
    						'post_type' 	 => 'events',
    						'posts_per_page' => -1,
    						'tax_query'      => array(
    					        array(
    					            'taxonomy' => 'seasons',
    					            'field'    => 'id',
    					            'terms'    => "2",
    					        ),
    					    ),
    			    	    'meta_query' => array(
    			                array(
    			                    'key' => 'driverresults_%_driver', // name of custom field
    			                    'value' => "126",
    			                    'compare' => 'LIKE'
    			                ),
    			            ),
    			    	    					    
    					);
    
    					$season_events = new WP_Query($args);
    					
    
    					if($season_events->have_posts()) : // If this driver has events in the current season
    
    						while($season_events->have_posts()) : $season_events->the_post(); ?>
    
    							Event!
    							<?php the_title(); ?>
    
    						<?php endwhile; ?>
    
    				<?php endif; ?>

    Removing the meta_query returns post data but when the meta_query is there the returned posts are empty.

    I’ve been trying to get this working for a few hours and having looked through other posts, this seems like it should be working?

    Any help would be much appreciated.

    Thanks,

  • Following on from this, I found that you need a function for using sub_fields in meta_query

    function my_posts_where( $where ) {
    						$where = str_replace("meta_key = 'driverresults_%", "meta_key LIKE 'driverresults_%", $where);
    						return $where;
    					}
    					add_filter('posts_where', 'my_posts_where');

    Hope this helps someone else!

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

You must be logged in to reply to this topic.