Support

Account

Home Forums General Issues Filter relationship field on taxonomy term page based on term

Solving

Filter relationship field on taxonomy term page based on term

  • I’ve added a relationship field to a taxnomyterm field, in order to select certain posts that i want to ‘feature’ on the term page.

    But this field returns all posts now, though i want to return only posts connected to that certain taxonomy.

    I thought i could do that by using this function:
    http://www.advancedcustomfields.com/resources/acf-fields-relationship-query/

    So i created a tax query, for testing (tested the function in general, posts per page is working):

    function my_relationship_query( $args, $field, $post )
    {
        // increase the posts per page
        $args['posts_per_page'] = 10;
        $args['tax_query'] = Array(
    	    'terms' => '30'
        );
    
        return $args;
        
    }

    But this still returns everything.

    So 2 questions:

    1 How to get this function right, so it returns only posts with certain term
    2 How to access the term id object in this hook, so it works dynamically?

    Maybe someone can help me out

  • Hi @timo

    For the tax_query, change your code to the following:

    function my_relationship_query( $args, $field, $post )
    {
        // increase the posts per page
        $args['posts_per_page'] = 10;
        $args['tax_query'] = array(
               array(
    	    'terms' => '30'
               )
        );
    
        return $args;
        
    }

    Check out this link for more information: https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

    For accessing the term object id dynamically, you could consider using get_queried_object() function. Here is the link to the documentation: https://codex.wordpress.org/Function_Reference/get_queried_object

  • The queried object isn’t working for me in that AJAX call. Also can’t access the $_REQUEST variables. Looking for a way to dynamically dial in the posts to the current tax as well…

  • I take that back. I could not access $_GET. I can access $_REQUEST (or $_POST specifically).

    I get this from $_REQUEST:

    Array
    (
        [paged] => 1
        [taxonomy] => 
        [post_type] => 
        [s] => 
        [max] => 
        [min] => 
        [action] => acf/fields/relationship/query
        [field_key] => field_5a5deb8769362
        [post_id] => term_7
        [nonce] => 5aa22de3aa
    )

    So you can use ‘post_id’ and replace ‘term_’.

    if ( ! empty($_REQUEST['post_id']) ) {
    	$args['tax_query'] = array(
    		array(
    			'terms' => str_replace( 'term_', '', $_REQUEST['post_id'] );
    		),
    	);
    }
  • Cleaned up basic customization below. The $_REQUEST['post_id'] is the same as the $post_id provided in the arguments.

    function custom_relationship_query( $args, $field, $post_id ) {
    	$args['tax_query'] = [
    		[
    			'taxonomy' => 'custom_taxonomy_name',
    			'field'    => 'term_id',
    			'terms'    => str_replace( 'term_', '', $post_id ),
    		],
    	];
    	return $args;
    }
    add_filter('acf/fields/relationship/query', 'custom_relationship_query', 10, 3);
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.