Support

Account

Home Forums Backend Issues (wp-admin) Help with relationship/query HOOK

Helping

Help with relationship/query HOOK

  • Hi there!

    I am trying to use the relationship/query HOOK, to filter the posts available in the Relationship field.

    I have two CPTs:
    – project
    – service

    I am using the awesome plugin CPT-onomies to create/register these cpts, so that the Service cpt can also be a taxonomy for the Project cpt.

    This works great. For example:
    – Project “Acme” has a Service taxonomy term “Consulting”.
    – “Consulting” also has a full (cpt) post of its own (unlike a normal taxonomy term, without using CPT-onomies).

    On this “Consulting” post/page, I want to show a CURATED LIST of the Projects that have the “Consulting” service.. so rather than ALL those projects (in which case I’d just to a wp_query), I want to select only SOME of those projects, and MANUALLY set the order. This is a perfect use of the ACF Relationship field.

    On the admin > edit-post screen for Consulting (remember, it’s really a cpt post), is my ACF Relationship field, in which the left side should show ALL Projects that have “Consulting” as a Service taxonomy term, from which i can make my curated selections (building a list on the right side).

    My ACF field setup is:

    – Field Name = curated_projects
    – Field Type = Relationship
    – Filter by Post Type = Projects
    – Filter by Taxonomy = (blank/all)
    – Filters = Search (yes); Post Type (no); Taxonomy (no)

    Then i can use the “relationship/query” HOOK to filter the Projects for the “Consulting” term.

    add_filter('acf/fields/relationship/query/name=curated_projects', 'my_relationship_query', 10, 3);
    function my_relationship_query( $args, $field, $post )
    {
    	$service_slug = 'consulting';  // make this DYNAMIC!!!
    
    	$args['tax_query'] = array( 
    			array(
    				'taxonomy' => 'service',
    				'field'    => 'slug',
    				'terms'    => $service_slug,
    			),
    		);
        return $args;
    }

    This works perfectly — when the tax-term is HARD-CODED (in the $service_slug var). But I can’t seem to make it work DYNAMICALLY, based on the slug of the post currently being edited (‘consulting’).

    The function gets the $post object, so i expected this would work:
    $service_slug = $post->post_name;
    but the result (in the Relationship field) is “No matches found”.

    I tried adding
    global $post
    but no luck.

    I tried just using the post ID from the url:

    	$service_id = $_GET['post'];
    	$service = get_post( $service_id );
    	$service_slug = $service->post_name;

    but again “No matches found”.

    Any ideas?? Why does it work perfectly when $service_slug is hard-coded, but not from the $post->post_name (slug)?

    Thanks in advance.

  • Hi @tex77,

    Thanks for the post.

    I am a bit lost in your description, shouldn’t you be passing the current term assigned to the post being edited?

    This can be achieved by the get_the_terms() function.

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

You must be logged in to reply to this topic.