Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Stefan Haas

Query for inline editing of ACF relationship fields

Hello!

is it possible to limit the selectable options when inline editing ACF relationship fields?

For example, I have an ACF relationship field that is restricted to top level posts only. This is done by using an ACF filter like this:

add_filter( ‘acf/fields/relationship/query/name=my_relationship_field_name’,’my_relationship_options_filter’, 10, 3);
function my_relationship_options_filter($options, $field, $the_post) {
$options[‘post_parent’] = 0;
return $options;
}

Unfortunately, child posts are still selectable when inline editing this ACF relationship field.
Is there any way to restrict it to top level posts as well, so that child posts are not selectable anymore?

11 months, 4 weeks ago
Stefan van den Dungen Gronovius
Developer

Our code does indeed not take the ACF hook into account.
But the results are fetched in an Ajax Call which can be targeted specifically by reading its params.
Together with the native WordPress hook ‘pre_get_post’ and a specific check on the WP_Query or based on the column ID, you could alter the query something like this:

add_action( 'pre_get_posts', function ( WP_Query $wp_query ) {
	if ( ! wp_doing_ajax() ) {
		return;
	}

	if ( filter_input( INPUT_GET, 'action' ) !== 'acp_editing_request' || filter_input( INPUT_GET, 'method' ) !== 'inline-select-values' ) {
		return;
	}

	// If you want to check for a specific column
	if ( filter_input( INPUT_GET, 'column' ) !== '646e1814cb020c' ) {
		return;
	}

	$wp_query->set( 'post_parent', 0 );

} );

The code need some adjustments for you to make it work, but I hope you get the point on what to do here.

11 months, 4 weeks ago
Stefan Haas

Hi Stefan, thanks for the quick reply! That means for the restriction of a certain column it should just look like this, or am I missing something?

add_action( 'pre_get_posts', function ( WP_Query $wp_query ) {

	// If you want to check for a specific column
	if ( filter_input( INPUT_GET, 'column' ) === '646e1814cb020c' ) {
		$wp_query->set( 'post_parent', 0 );
	}

} );
11 months, 4 weeks ago
Stefan van den Dungen Gronovius
Developer

Correct, but you probably only want to do this when an Ajax call is fired and when the specific editing call fires.
My advice is to target as specific as possible.

11 months, 4 weeks ago

You must be logged in to reply to this topic.