Support

Account

Home Forums Add-ons Repeater Field Query all repeater fields and show parent data too

Solved

Query all repeater fields and show parent data too

  • I’m trying to build a festival website that has multiple events each year. Each event can occur more than once within each year.

    I have a post type called Events with a repeater field for Event times. In the repeater field there are two sub-fields, one for Start time and one for End time.

    I’m trying to display a chronological list of all the Events based on their Event times and have the following code (taken from https://www.advancedcustomfields.com/resources/query-posts-custom-fields):

    <?php 
    // filter
    function my_posts_where( $where ) {
    	$where = str_replace("meta_key = 'event_time_%", "meta_key LIKE 'event_time_%", $where);
    	return $where;
    }
    
    add_filter('posts_where', 'my_posts_where');
    
    // set festival year in YYYY
    $date = $festival_year;
    
    // args
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'	=> 'event',
    	'meta_query'	=> array(
    			'key'		=> 'event_time_%_start_time',
    			'compare'	=> 'LIKE',
    			'value'		=> $date
    	)
    );
    
    // query
    $the_query = new WP_Query( $args );
    ?>
    <?php if( $the_query->have_posts() ): ?>
    	<ul>
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<li>
    			<a href="<?php the_permalink(); ?>"><?php the_title(); ?> - <?php the_sub_field('start_time'); ?></a>
    		</li>
    	<?php endwhile; ?>
    	</ul>
    <?php endif; ?>
    
    <?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>

    This successfully finds all the Events that have Start times in the $festival_year. The problem I am having is that I can’t get the code to show the_sub_field('start_time') for each Event. I would also expect to see some of these Events duplicated as two of them have two Event times fields, both in the same year.

    I am essentially trying to query all sub_field data first and then connect the associated post data (title, content, etc). Then I can output it as a calendar.

    Is this possible?

  • A standard WP_query, no matter what you set for arguments, will not list the same post twice, that’s the way it works. In order to do what you want to do you’ll need to look into using $wpdb and construct your own MySQL queries https://codex.wordpress.org/Class_Reference/wpdb.

  • I thought it might something more than WP_Query.

    I’ll look into $wpdb. Thanks

  • @chris_huh I know it’s been a while since you wrote this, but did you manage to solve it? I’m trying to do the same thing and haven’t been able to.

    I would appreciate if you could share your code.

    Thanks!

  • More recently I outlined how this can be done using a hierarchical post type. The idea is here https://support.advancedcustomfields.com/forums/topic/helping-planning-acf-project/ but there is no code with it.

  • Hey @hube2, thanks for your reply.

    I checked what you did with the child posts and that might work, but it’s overkill for what the client needs (and what the budget allows me to do :P). But what you said about allowing the different event dates to have a different description made me think that I might simplify this further by just installing a duplicate post plugin and using the standard WP_query.

    Thanks again!

  • Hi @camilolunacom ,

    I had a quick look back at the code I ended up with and it looks like I used $wpdb to do this. That allowed the same post to be returned multiple time, but for the results to be sorted by the repeating event_time field.

  • @camilolunacom It was overkill for the project I did it on too and put be over budget (had to eat the time) However, it was a feature that we can reuse so worth the extra effort for us.

Viewing 8 posts - 1 through 8 (of 8 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.