Home Forums General Issues Querying a custom field from a category


Querying a custom field from a category

  • I have a query string i’m running to call a load of information about a category before a loop of posts. I have created a custom field which I have added to the category taxonomy and for some reason or another I can’t seem to force the contents of the custom field to appear. The custom field is only to create a URL for a link on a button.

    I’m using the following PHP to call the correct field.. but its not displaying: <?php echo $member_group_term->mep_party_link; ?>

    I have used the other ones like name and description and they display ok.. but the custom field doesn’t seem to want to display at all.

    Can anyone help me?

  • Custom fields are not stored as part of the standard WP term information and they cannot be accessed like this. This is similar to to custom fields on posts.

    For posts you need to use get_post_meta() to get the field in WP. With ACF you use get_field(). For term custom fields you still use the same ACF function but you must supply a post ID. You wan to echo the value so you can use the_field()

    $post_id = $member_group_term->taxonomy.’_’.$member_group_term->term_id;
    the_field(‘mep_party_link’, $post_id);

  • Thank you so much for that!! it worked a treat 😀

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

The topic ‘Querying a custom field from a category’ is closed to new replies.