Support

Account

Home Forums General Issues Relationship field: seems like altering the query doesn't work

Solving

Relationship field: seems like altering the query doesn't work

  • Hello!

    I’m trying to alter the query of the relationship field so that it only returns pages of a specific template. However, I seem to get the same result no matter how I alter the query, it just returns the posts on the site.

    Now I have this:

    function my_relationship_query( $args, $field, $post_id ) {
    	
    	// only show children of the current post being edited
    	$args['post_parent'] = 7;
    
    	// return
    	return $args;
        
    }
    // filter for every field
    add_filter('acf/fields/relationship/query', 'my_relationship_query', 10, 3);

    It is more or less taken from the docs. I still just get the posts on the page. 7 is the id of a page.

    What I was trying to do originally, was something like this:

    return array(
        'post_type' => array('page'),
        'meta_query' => array(
        array(
          'key' => '_wp_page_template',
          'value' => 'page-theme.php',
          'compare' => '='
        )
      )
    );

    inside the filter function to get the pages I wanted, but that also returned the posts on the page.

    Can anyone help?

    If I remove the filter, it displays all the pages and posts, if I add any filter, it seems to just list out the posts.

  • What version of ACF are you using?

  • Hello, John!

    I have version 5.4.3.

  • I tried the example from the docs and it seems to be working as expected

    
    function my_relationship_query( $args, $field, $post_id ) {
    	
    	// only show children of the current post being edited
    	$args['post_parent'] = $post_id;
    
    	// return
    	return $args;
        
    }
    // filter for every field
    add_filter('acf/fields/relationship/query/name=relationship_query_test', 'my_relationship_query', 10, 3);
    

    If you are still having trouble with this then try deactivating other plugins and/or switching to an unmodified 20XX theme to see if there is still a problem. Something else could be interfering with the query.

    As far as altering the query to get pages that use a specific template, you will have problems with this due to the way ACF sorts the post when the post type is hierarchical. Basically, in this case it really can’t be done. ACF attempts to order the posts in hierarchical order and if the parent of a page is not returned because of the filter then the results will be incorrect. So, the ability to filter the query can be limited on hierarchical post types.

  • Ah, I found my mistake. I did what you said, disabled the plugins and changed the theme. That worked out.

    When I tried to changed it back to the theme we’re using, and put the code in functions.php, earlier it was in another file, it worked.

    Then I put it in the original file which gets included in the functions.php file, and it stopped working. Then, I noticed I forgot to add the namespace before the callback.
    So this works:

    function my_relationship_query( $args, $field, $post_id ) {
    	
    	// only show children of the current post being edited
    	$args['post_parent'] = 7;
    
    	// return
    	return $args;
        
    }
    // filter for every field
    add_filter('acf/fields/relationship/query/name=_theme', __NAMESPACE__ . '\my_relationship_query', 10, 3);

    I’ll change the id of the post to a field which they can set on an options page.

    Sorry about that, and super thanks for your help!
    Hope I didn’t take to much of your time 🙂

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

The topic ‘Relationship field: seems like altering the query doesn't work’ is closed to new replies.