0

I have been stuck on the following case for a while now. On a page I have a list of tags which I try to filter. When the post for example has an ID of 10586, the only tag displayed should be 10586.

enter image description here

The name of the taxonomy is post_tag and the value of the term is the same as the ID of the post its on. A post with the ID of 10586, has a term named 10586.

I tried to accomplish that with this code:

function my_acf_load_field( $field )
{
    global $post;
    $post_id = get_the_ID();
    $field['taxonomy'] = array();
    wp_reset_query();
    $query = new WP_Query(array(
        'post_type' => 'actor',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'posts_per_page' => -1,
        'tag' => $post_id,
        'tax_query' => array(
            array( 
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' => 'post_id'
            )
        )
    ));             
    
    foreach($query->posts as $product_id=>$macthed_product){
            $choices[$macthed_product->ID] = $macthed_product->post_title;
    }
    
    $field['taxonomy'] = array();

    if( is_array($choices) )
    {
        foreach( $choices as $key=>$choice )
        {
            $field['taxonomy'][$key] = $choice;
            if($choice->name == $post->ID){
                $choice = get_the_terms( $post->ID, 'taxonomy' );
            }
        }
    }

    wp_reset_query();
    return $field;
    
}
add_filter('acf/load_field/name=actor_tags_actor_profile', 'my_acf_load_field');

After running the code in functions.php it returns the tags as in the image above. Any help with this is greatly appreciated.

2
  • You've confused tag/term IDs and tag slugs/names, 10586 is not the ID of the tag, it is the name of the term. tax_query also expects term IDs, not the word post_id, and wp_reset_query should only be used to clean up after a query_posts call
    – Tom J Nowell
    Jul 7, 2021 at 16:02
  • @TomJNowell Sorry for the late response. Thank you for the feedback, I shall make adjustments to my code. Jul 8, 2021 at 6:55

2 Answers 2

0

Can you try to escape the variable $post_id?

Like so: ''.$post_id.''

So use the single quote twice.

You are querying a slug and your variable is a number.

1
  • I tried to escape $post_id in functions.php, but It did not work. Do I need a function to escape it? Jul 8, 2021 at 7:40
0

I ended up changing the tags for a checkbox and applied the following code to only show the checkbox that has a value that is equal to the one of the post it is displayed on.

function my_acf_load_field( $field )
{
    global $post;
    $post_id = get_the_ID();
    $field['choices'] = array();
    wp_reset_query();
   $query = new WP_Query(array(
        'post_type' => 'projectsoverview',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'posts_per_page' => -1,
        'tag' => $post_id
    ));             
    
    foreach($query->posts as $product_id=>$macthed_product){
            $choices[$macthed_product->ID] = $macthed_product->post_title;
    }
    
    $field['choices'] = array();

    if( is_array($choices) )
    {
        foreach( $choices as $key=>$choice )
        {
            $field['choices'][$key] = $choice;
        }
    }
    
     wp_reset_query();
    return $field;
    
}
add_filter('acf/load_field/name=actor_tags_actor_profile', 'my_acf_load_field');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.