0

I will try to elaborate the title. I want to make a post, within this post I want to loop through all posts from a custom taxanomy and extract custom fields.

How is this done in the most efficient way?

What I am thinking about is using the template hierarchy: single-post-{slug}.php

Within this I get the content using:

<?php get_template_part( 'content', '{custom template}' ); ?>

So in content-custom-template.php I will make the loop that goes through all the custom taxanomies.

So as you can see, this will require 2 additional template files for every custom taxanomy I want to do this with (because I need a post for every custom taxanomy).

What I need to know:

  • Is there are more efficient/better way?
  • How do I make the loop for custom taxanomies within the post?
  • If I am understanding your question correctly, then you would want to create a new loop with a custom WP_Query and within that loop grab your custom fields? – Cedon Nov 6 '16 at 14:02
  • That is correct ! – 2by Nov 6 '16 at 14:10
1

You are going to need set up a new WP_Query in your content-*.php with the correct parameters in order to send it into a loop.

<?php
   $args = array(
     'tax_query' => array(
             array(
                    'taxonomy' => 'recipes',.
                    'field'    => 'slug',
                    'terms'    => 'dessert'
   )));

   $your_posts = new WP_Query( $args );
?>

You can add any other parameters you want/need for this query. You can find more reference on parameters at the WordPress Codex page for WP_Query and add them to the $args array.

In my example, WP_Query is going to look at the recipes taxonomy, it's going to find recipes by looking at the slug, and find all posts with the value dessert.

Once you have the new WP_Query set up, you then send it into the WordPress loop:

<?php
    if ( $your_posts->have_posts() ) : while ( $your_posts->have_posts() ) : $your_posts->the_post();
?>
// Your Post Stuff Goes Here
<?php endwhile; else: ?> <p>Sorry, there are no posts to display</p>
<?php endif; ?>
<?php wp_reset_query(); ?>

In order to get at your custom fields, I would suggest writing a function in your functions.php file to handle this and call it within the loop. That way if you can use it throughout the theme with more ease.

<?php
   function my_custom_field( $id, $field_name ) {
      return get_post_meta( $id, $field_name, true );
   }
?>

What this function does is access the post's metadata, look for the field name you specify, and returns a single value. If there are multiple values using the same key, then omit the true and the function will instead return an array which you can then use a foreach() to process that.

So, for example, say your custom field is 'mood' then the call to the function would be:

<?php my_custom_field( $post->ID, 'mood' ); ?>
  • I <3 you, thank you so much! (i corrected some minor mistakes giving PHP errors btw). :) – 2by Nov 6 '16 at 17:58
  • You're welcome. I didn't have time to test it in my environment since I do not have any custom taxonomies set up. Out of curiosity, what was generating errors? – Cedon Nov 6 '16 at 18:06
  • I have made an edit with the correction. It was just some missing ")" in the first script. – 2by Nov 6 '16 at 19:06
  • That's what I get for typing code in notepad :). Thanks for making the correction, I approved it so it's now part of the solution. – Cedon Nov 6 '16 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.