Support

Account

Home Forums ACF PRO Get values from Taxonomy terms on archive.php page = NULL

Solved

Get values from Taxonomy terms on archive.php page = NULL

  • Hello

    I’m having trouble outputting the values from a taxonomy within an archive page.

    WP set up

    CPT called “members”. Taxonomy linked to this called “jobs”.

    ACF set up

    http://cl.ly/1X0u1o1U0H0x
    http://cl.ly/1F1Z2h2f2v3G

    Template archive-members.php

    while ( have_posts() ) : the_post(); 
    $queried_object = get_queried_object(); 
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;  
    
    $job_title = get_field('member_person_job', $taxonomy . '_' . $term_id);
    
    echo $job_title;
    
    echo '<div style="background: #000;"><pre>';
    var_dump( $job_title );
    echo '</pre></div>'; 
    endwhile;

    Browser errors + var dump

    http://cl.ly/1M2a0C0r023s

    Just getting NULL from the var dump and the PHP warnings, with no taxonomy values being output.

    Any ideas?

    Thanks
    Dave

  • What are the location rules for the field group? Is it a taxonomy term or a post type?

  • If it’s a post type you shouldn’t be using the taxonomy and term id for post id, and since in inside the loop you don’t need the post id at all.

    What you’ll get for output depends on what you have the field set to return.

    
    while ( have_posts() ) : the_post(); 
    
    $job_title = get_field('member_person_job');
    
    echo $job_title;
    
    echo '<div style="background: #000;"><pre>';
    var_dump( $job_title );
    echo '</pre></div>'; 
    endwhile;
    
  • OK that makes some sense.

    So if I have it set to return the ID, how do I then get the term, currently it outputs the ID number via the echo, so presumably I’d need something like:

    echo $job_title->term_id;

    But then I’m back to using this block:

    $queried_object = get_queried_object(); 
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;  

    Which produces the errors.

  • What you have there won’t work, the queried object is a list of posts, unless you are in the template to show that particular taxonomy. Instead you should use get_term() https://codex.wordpress.org/Function_Reference/get_term

    `
    $job_term = get_term(get_field(‘member_person_job’), ‘taxonomy-name’));

  • Thank you.

    Using this:

    $job_term = get_term(get_field('member_person_job'), 'taxonomy-name');
    
    echo $job_term;

    Produces this error:

    http://cl.ly/2v0V0I0l203n

    However, I can get the term using this:

    $job_term = get_the_term_list( $post->ID, 'jobs', '', ',' ); $terms = strip_tags( $terms );

    Based on your comment, the original block used on taxonomy-jobs.php should work, but this still results in NULL:

    $queried_object = get_queried_object(); 
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;  
    
    $job_title = get_field('member_person_job', $taxonomy . '_' . $term_id);
    
    echo $job_title;

    By all accounts this should work on the taxonomy page?

  • The first one creates an error because the taxonomy “taxonomy-name” does not exist. I didn’t know the name of the taxonomy and you needed to replace that with the name of your taxonomy

    $job_term = get_term(get_field('member_person_job'), 'jobs');

    The last block of code will not work. The currently queried object is probably not a taxonomy. The only way that will work would be on a url that looks something like http://www.yourside.com/jobs/JOB_NAME_HERE

  • Thank you.

    I’ve got this into a position I need, so thanks, but for the sake of this thread being useful, I should say that this:

    $job_term = get_term(get_field('member_person_job'), 'jobs');
    echo $job_term; 

    Results in this error on the archive page:

    http://cl.ly/3m1Z0D1L0R3L

  • That’s because you’re trying to echo an class object.

    
    $job_term = get_term(get_field('member_person_job'), 'jobs');
    print_r($job_term);
    

    will show the values of the object

  • Thanks, that makes sense.

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

The topic ‘Get values from Taxonomy terms on archive.php page = NULL’ is closed to new replies.