wp query – Only show tag with the same id as the post

Question

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.

0
codeminator 3 weeks 2021-07-07T10:11:28-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse