Dan Worlds
Dan Worlds

Reputation: 35

How to Get Wordpress Attachment Taxonomy Data

I've used the method on the following page to create a Taxonomy named Location.

http://code.tutsplus.com/articles/applying-categories-tags-and-custom-taxonomies-to-media-attachments--wp-32319

How can I get the data from the field named Location and return its value. Please help! Thanks

Upvotes: 3

Views: 1693

Answers (1)

Ashish Patel
Ashish Patel

Reputation: 3614

As per tutorial given in URL above consider register taxonomy name 'location' for media section.Below code will help to fetch data from field location

$tax_terms = get_terms('location', array('hide_empty' => false));
foreach($tax_terms as $term)
{
    $termcat  = $term->term_id;
    $term->name;
    $post = get_posts(array(
    'post_type' => 'attachment',
    'tax_query' => array(
    array(
    'taxonomy' => 'location',
    'field' => 'term_id',
    'terms' => $termcat)
    ))
    );

    foreach ($post as $mypost) 
    {
        echo $mypost->post_title . '<br/>';
        echo $mypost->post_content . '<br/>';
        echo  $mypost->ID . '<br/><br/>';
        echo  $mypost->ID . '<img src="'.$mypost->guid.'" width="150" height="150" />';
    }
}

Upvotes: 3

Related Questions