Skip Navigation

[Resolved] Using Views with Advanced Custom Fields Relationship Field

This thread is resolved. Here is a description of the problem and solution.

Problem: I am using ACF relationship fields to create links between two custom post types - Practices and Events. The Events CPT contains a Repeating Field Group (RFG) from Types. One of the fields in that RFG is a custom date field. On the Practice Area single post, I would like to display all the RFGs that occur on or after today, and also belong to an Event related to the current Practice Area post in the ACF relationship.

Solution: Use custom code to determine the IDs of the Event posts that are related to the current Practice Area. Not sure how that works, so consult the ACF documentation.

RFGs are essentially children in a parent/child relationship with the post where they are saved. Use the toolset_get_related_posts API to determine which RFG post IDs are associated with those Event IDs, and push those RFG IDs into an array. Pass those IDs into a View of RFG's post ID filter using render_view. Add a custom field date filter to the View of RFGs by date in addition to post ID. If no related RFG IDs are found, do not render the View.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
http://php.net/manual/en/function.sizeof.php

This support ticket is created 6 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 20 replies, has 2 voices.

Last updated by keithW 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1076525

- Uncheck "Don't include current page in query results". This checkbox can cause problems when it's not needed. It's only needed if you want to display a View of some post type on a single post of the same post type. Your View and its placement are different post types, so the checkbox is not needed.
- I cannot see the Query Filter section. If you cannot see the Query Filter section, scroll to the top right corner of the screen and click "Screen Options". You can enable the panel here.
- Check the Query Filter settings. It should NOT include a post relationship filter. It should include a post ID filter, set by the shortcode attribute "ids". It should also include a custom field filter by the RFG date field, where the custom field date is greater than or equal to today's date, I assume, if you do not want to show old Event RFGs.
- Check the Order settings. I'm not familiar with your custom date field names, but this should be the Event RFG date field, not a date field on the Event post or any other date field.
- Check the Template. I'm not familiar with your custom date field names and locations, but remember this is a View of Event RFGs. To display information from the RFG, you can simply insert a Types field shortcode for the custom field. To display information from the parent Event, or the current Practice Area, you'll have to use the post selection tab when inserting those shortcodes.

#1076536

Christian,

I unchecked the "Don't include current page in query results" box.

The Query filter is:

Include only posts with IDs set by the View shortcode attribute "ids" eg. [wpv-view name="view-name" ids="1"]

I'm leaving the custom field filter on date out right now so we can test with the full database of events.

Ordering is correct on the right date field.

Template should be fine.

Still no results though. How do we troubleshoot to see what's not working?

#1076538

Let's try using a string in the ids parameter instead of a native PHP array:

$args2 = array(
                    'id' => 3224,
                    'ids' => implode(',', $pulsanti),
                    'showposts' => 3,
                );
                echo render_view( $args2 );

If this still doesn't work, please provide login credentials in the private reply fields here and I'll figure out what's going on.

#1077590

We are very close to having this completed... thanks again!

The only issue I'm seeing right now is if there are no events associated to the array, it seems to be displaying the latest 3 events instead of nothing at all.

For example on this page hidden link, no events should be displaying.

The current code looks like this:

<?php
               $posts = get_posts(array(
					'post_type' => 'event',
					'showposts' => 3,
					'meta_query' => array(
						array(
							'key' => 'practice_areas', // name of custom field
							'value' => '"' . $theid . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
							'compare' => 'LIKE'
						)
					)
				)); ?>
					<?php $pulsanti = array();
					foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
						<?php setup_postdata($post);
			    		$event_id = get_the_ID(); 
			
						$event_ids .= "$event_id,"; ?>
						
						<?php
			
						$pulsanti[] = toolset_get_related_posts(
						  $event_id,        // the parent RFG
						  'event_dates',  // the RFG slug
						  'parent',     // the sezioni role in this relationship is 'parent'
						  1000000,     // the maximum number of results
						  0,           // the offset
						  array(),     // additional query arguments
						  'post_id',   // return format
						  'child'    // role to return
						);
						?>
						
					<?php endforeach; ?>
					<?php 
					$pulsanti = array_merge(... $pulsanti);
					$pulsanti = array_slice($pulsanti, 0, 3);
					?>
        <?php
				$args2 = array(
					'id' => 3224,
					'ids' => implode(',', $pulsanti),
					'showposts' => 3,
				);
  				echo render_view( $args2 );
			
				$posts2 = get_posts(array(
					'post_type' => 'event',
					'showposts' => -1,
					'meta_query' => array(
						array(
							'key' => 'practice_areas', // name of custom field
							'value' => '"' . $theid . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
							'compare' => 'LIKE'
						)
					)
				));
			
				$num2 = count($posts2);
		?>
			
          <?php if ($num2 > 3) { ?><a class="button-secondary-blue" href="/events#search-news-capabilities-<?php echo "$pa_title"; ?>">View More</a><?php } ?>
#1077611

Okay great, then I think you should set up a conditional to check the size of the $pulsanti array using sizeof() before you call echo render_view(). If the size is 0, do not call the render_view function. There's no reason to call it with an empty ID filter.

http://php.net/manual/en/function.sizeof.php

#1078734

Thanks for helping fix the issue!