Support

Account

Home Forums Front-end Issues Pulling values from taxonomy array

Solved

Pulling values from taxonomy array

  • Hello-

    I need some help pulling values from an array. Here is the situation:

    I have a custom post type (projects) where I have used a taxonomy custom field (projects_category), which is attached to a specific posts category (SLIS: Second Mastery Demonstration Paper).

    When I do a var_dump…

    
    $values = get_field('projects_category');
    var_dump($values);
    

    …I see the array keys and values:

    
    object(stdClass)#317 (9) { ["term_id"]=> string(2) "16" ["name"]=> string(40) "SLIS: Second Mastery Demonstration Paper" ["slug"]=> string(39) "slis-second-mastery-demonstration-paper" ["term_group"]=> string(1) "0" ["term_taxonomy_id"]=> string(2) "17" ["taxonomy"]=> string(8) "category" ["description"]=> string(0) "" ["parent"]=> string(2) "15" ["count"]=> string(1) "6" }
    

    I need to pull a number of the values and create variables out of them in order to build some specific URLs and also to develop a query that pulls the latest posts from this particular category.

    I’ll admit it: my PHP skills are weak, and this seems relatively straightforward. But my scouring here at ACF, WordPress forums, and Stack Exchange didn’t get me much.

    Could anyone help me out a bit? I’ve seen bits of this question asked elsewhere, so I’m sure the answers you provide will be helpful to others as well.

    Thanks,
    ~Kyle~

  • Hi Kyle,

    I’ve been doing some work with this field type lately and have gotten to know how to work with it a little more. Can you give me a little more information on what you need to do? Fill out the scope a little bit?

    Also, as a sidenote, you may already be on top of this, but you can format your var dumps so they’re easier to view:

    $values = get_field('projects_category');
    print '<pre>';
    print_r( $values );
    print '</pre>';

    Thanks,

    Jeff

  • Hi @thecorkboard

    When you say “I need to pull a number of the values and create variables out of them in order to build some specific URLs and also to develop a query that pulls the latest posts from this particular category.”, I don’t understand what you are asking.

    Can you be very clear and specific?

    Thanks
    E

  • Hello @elliot and @jeffvls:

    Thanks for the hint on the var dumps, @jeffvls. As for the scope, let me see how I can explain this a bit clearer. So, I’m a doctoral student and I want folks to know what projects I’m working on. To do so, I’ve created a ‘projects’ custom post type. I will be writing posts that are categorized with a category that is similar if not the same title to the project I’m working on. So, I’ve used the taxonomy field to pick the category to attach to the individual project.

    There are two things I want from this category relationship:
    1) I want to echo the title of the category and then link to the category’s archive of posts;
    2) I want to list maybe the last 5 posts from that category


    @elliot
    , does that give you a bit more information as well?

    I’m appreciative of any help you can provide.

  • Hi Kyle,

    Thanks for the additional explanation. Drop this into the code (within the loop) and give this a shot real quick to see if this outputs the category name and slug correctly. If so, you can then take the ‘category slug’ to generate a custom WP Query to grab the 5 most recent posts by category. If this doesn’t work it’s because I grabbed it from my code and attempted to modify it for your purposes without actually testing… : )

    Just let me know if it doesn’t work and I’ll see what I can do. Here’s the code snippet:

    <?php 
    
    $categories = get_the_terms( $post->ID, 'category' );
    if ( $categories && ! is_wp_error( $categories ) ) : ?>
    
      <ul>
      		      
      <?php foreach ( $categories as $category ) : ?>
        
        <li>Category Name: <?php echo $category->name; ?> /  Category Slug: <?php echo $category->slug; ?></li>
      
      <?php endforeach; ?>
        
      </ul>
      
    <?php endif; ?>
  • Hmmm, nope @jeffvls, that didn’t seem to work. But I’ll take what you wrote and try to build off of it.

    Thanks!

  • Hi Kyle,

    Sounds good. Let me know if you need me to take another shot at it.

    Thanks,

    Jeff

  • @jeffvls

    Well, if you have ideas, please do feel free to throw them out and I’ll provide feedback. I was up late last night trying a few more things, but I wasn’t successful.

    Thanks,
    ~Kyle~

    p.s. if I could award you “reputation” points, I would. I appreciate your help.

  • Hi Kyle, I recreated the situation over here and have some verified code. Here’s a complete, but simplified page template (‘single-project.php’):

    <?php get_header(); ?>
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    			
    			<h1><?php the_title(); ?></h1>
    				<?php the_content(); ?>
    
            <?php 
            // Grab all of the 'category' terms that have been applied to this 'project' and 
            // go through and output the category title and any posts that fall under the category.
            $categories = get_the_terms( $post->ID, 'category' );
            if ( $categories && ! is_wp_error( $categories ) ) : ?>
            
              <?php foreach ( $categories as $category ) : ?>
                
                <h3><?php echo $category->name; ?>:</h3>
               
                <?php // Generate a custom query using the category's slug
                $relatedPosts = new WP_Query( array('category_name' => $category->slug, 'posts_per_page' => -1) );
                if ( $relatedPosts->have_posts() ) : ?>
                
                  <ul>
                  
                  <?php while ( $relatedPosts->have_posts() ) : $relatedPosts->the_post(); ?>
                  
                    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
      
                  <?php endwhile; ?>
                  
                  </ul>
                
                <?php endif; 
                // Restore the global $post variable to the current post in the main query
                wp_reset_postdata(); ?>
              
              <?php endforeach; ?>
            <?php endif; ?>
    
    	<?php endwhile; endif; ?>
    
    <?php get_footer(); ?>

    As I was setting this up, I realized that it’s possible that the earlier snippet may not have worked for you because of the way the options may be set in the custom field definition. When adding the ‘taxonomy’ custom field, make sure that the field is set to return the ‘Term Object’ instead of the ‘Term ID’ so that it will work with the above code block.

    OK, let me know how this works.

    Thanks,

    Jeff

  • Hi @jeffvls-

    This worked great, but only after I had selected “Load value based on the post’s terms and update the post’s terms on save.” I had already set it to grab the term object.

    This really is such a blessing. Thanks for your wonderful help. Much appreciation is coming your way!

    ~Kyle~

  • Hi Kyle,

    Glad that helped! Makes sense with having “Load value based on the post’s terms and update the post’s terms on save” selected…

    Have a good one.

    Jeff

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

The topic ‘Pulling values from taxonomy array’ is closed to new replies.