Support

Account

Home Forums General Issues Get taxonomy name from 'Taxonomy' field and insert into custom loop as category?

Solving

Get taxonomy name from 'Taxonomy' field and insert into custom loop as category?

  • Hi guys,
    Struggling a bit here and need some help I have read through the documentation but I just can’t seem to get the information I need.

    I have a custom post type and I have created a page template with a loop to get posts from this custom post type. This is all done fine.

    I have 10 categories associated with this CPT, and I am creating a page for each of these categories to display the associated category posts along with other information. I only want to create 1 page template with an easy way to link up the pages to display the associated category for this CPT.

    So for this template I have added the ‘Taxononmy’ field from ACF. So on each edit page I can select the associated category with that page. So everything that is returned is relevant.

    See my loop code below and if any one can tell me how I pass this ‘Taxonomy’ info into my loop based on using the ‘Taxonomy’ field in ACF I would be most thankful!

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array (
    	'post_type'	=> 'exercise-database',
        'taxonomy'  =>  ,
        'pagination'	=> true,
    	'paged'         => $paged,
    	'order'		=> 'DESC',
    ); ?>
    
    <?php global $wp_query;
    $wp_query = new WP_Query( $args ); ?>
    
    <?php if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
  • Do your have the field set to save and load terms?

    the taxonomy query added to your arguments should look something like this: see https://codex.wordpress.org/Class_Reference/WP_Query

    
    $args = array (
        'post_type'	=> 'exercise-database',
        'tax_query'  =>  array(
          array(
            'taxonomy' => 'your taxonomy here',
            'terms' => array($term_id), // <= this needs to be an array of term id, can have a single value
            'operator' => 'IN'
          )
        ),
        'pagination'	=> true,
    	'paged'         => $paged,
    	'order'		=> 'DESC',
    );
    

    But before you go to the trouble of creating a custom page template for every category you should think about using the template hierarchy built into WP. If you have a custom post type and you add a custom taxonomy for the CPT then you only need to create 3 template files: archive-{$custom-post-type}.php, single-{$custom-post-type}.php and taxonomy-{$custom-taxonomy}.php. It would be a lot easier than building and doing custom queries for every category.

  • Thanks John. I will try this in the morning and let you know.

    In regards to templates, what you have suggested is exactly what I’ve done and what I was avoiding was making more then one ‘page template’ file.

    When I refered to creating pages for the 10 categories I meant in the sense of adding 10 pages via the wp backend. I would then use the one page template I’ve created called ‘Exercise Category’ this page template would have the ‘Taxononmy’ field from ACF so I could link the two; page and the specific category loop together. Thus meaning I dont have to create 10 page templates and hard code the category into each loop on each page.

    I hope im making sense!

    Appreciate the help

  • I was confused when you said “page templates” If you have a template for taxonomy-{$custom-taxonomy}.php then you don’t need to do a query to get the posts in the custom taxonomy. WP already does that for you, you just need to create a loop to show the posts, basically the same as category.php, you can even copy that to start from.

  • Hi John,
    I’m not sure if I’ve over complicated things here or not but I have tried your code above replaced the taxonomy name with my own taxonomy and have set on the ACF field, save and load terms to yes and I have return value set to Term ID but I just get a posts not found message. I have double checked that I have selected the correct category in the Taxonomy field on the edit page screen and double checked I have posts associated with that category but still shows posts not found.

    I tried returning post object, as well as a mixture of save and load terms…

    Little confused by your last message but again it may be me not explaining it correctly so I will do my best below to outline exactly what it is I am trying to achieve.

    I have a custom post type with custom taxonomies (categories and tags). This is all working correctly no problems here I can get them to display in a loop perfectly as well.

    This CPT is called ‘Exercise Database’, I then have 2 taxonomies associated with it:

    Exercise Categories
    Exercise Tags

    Inside Exercise Categories, I have created 10 different categories for different body parts. Chest, back, abs… and so on.

    I have also created a separate ‘Page Template’ called ‘Exercise Category Template’, this has nothing to do with necessary CPT files ‘single-‘, ‘archive-‘ and ‘taxononmy-‘. This is a theme based template that can be used on multiple pages via the add pages screen.

    The reason I need this page template is because: For each body part (category within the taxonomy) I need a separate page of information as well as the ability to only display exercises for that part of the body. I don’t want chest exercises on the back page. I only want to show the posts with the category of chest for the chest page.

    Hence my need for the custom loop because I need to pass the ‘back’ category from the custom taxonomy ‘Exercise Categories’ into the page that I have about back exercises. This is the same for each of the 10 pages.

    So I used the ‘Taxonomy’ ACF field so that I could hopefully insert the correct category back, chest or whichever one I needed into the loop and then the page only shows the posts from whichever category I have selected.

    Hopefully this will give you a thorough understanding of what I am trying to achieve, I am sorry I have only just started working with PHP but hopefully this will help!

    Thank you

  • Yes, that makes more sense. If I’m understanding correctly then the changes to the query that I posted should be working. Maybe it has something to do with using the global $wp_query variable, you shouldn’t be doing that.

    also, just to be sure, did you change your taxonomy here to the slug for your custom taxonomy and $term_id to the term ID that you want to return?

    
    <?php
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $args = array (
        'post_type'	=> 'exercise-database',
        'tax_query'  =>  array(
          array(
            'taxonomy' => 'your taxonomy here',
            'terms' => array($term_id),
            'operator' => 'IN'
          )
        ),
        'pagination' => true,
        'paged' => $paged,
        'order' => 'DESC',
      );
    
      $my_query = new WP_Query( $args );
      if ($mny_query->have_posts()) {
        while ($wp_query->have_posts()) {
          $wp_query->the_post();
          ?><h2><?php the_title(); ?></h2><?php 
        } // end while
       wp_reset_postdata();
      } // end if 
    
  • Hi John,

    Yes I changed ‘your taxonomy here’ to ‘exercise-categories’ which is my Taxonomy.

    Then this is what I am getting confused on, the $term_id this is what I am trying to pull in from the ACF Taxonomy Field. So on my edit page I select the category I want to pull in lets say ‘Back’ which is a category inside the Taxonomy ‘Exercise-Categories’.

    How do I pull it in?

    Thanks

  • Ok I just got it working using this!

    <?php
                    
                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    $term_id = get_field('exercise_category') ;
                      $args = array (
                        'post_type'	=> 'exercise-database',
                        'tax_query'  =>  array(
                          array(
                            'taxonomy' => 'exercise-categories',
                            'terms' => array($term_id),
                            'operator' => 'IN'
                          )
                        ),
                        'pagination' => true,
                        'paged' => $paged,
                        'order' => 'DESC',
                      );
                ?>
    
                                    
                <?php global $wp_query; $wp_query = new WP_Query( $args ); ?>
    
                        <?php if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    Looks like I needed to get the field and return as term_id. Im glad that works!
    Could you let me know why you advise not to use ‘global $wp_query’. I think the reason I am using that is something to do with the CPT, but I am all ears…

    Thanks John

  • Honestly, I’m not sure of the effects that using $wp_query will have. It may have no effect in your case, but in other cases it could. It’s simply safer and a better practice to use a new variable when creating a nested query/loop.

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

The topic ‘Get taxonomy name from 'Taxonomy' field and insert into custom loop as category?’ is closed to new replies.