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
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
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
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
Hi James,
I solved it with a little tweak to your code, thanks for getting me pretty much there!
<?php if( have_rows('factsheets') ): ?>
<div class="vc_row wpb_row vc_row-fluid dt-default" style="margin-top: 0px; margin-bottom: 0px; min-height: 0px;">
<?php while( have_rows('factsheets') ): the_row();
// vars
$factsheet_title = get_sub_field('factsheet_title');
$factsheet_pdf = get_sub_field('factsheet_pdf');
?>
<div class="wpb_column vc_column_container vc_col-sm-3">
<div class="vc_column-inner ">
<div class="wpb_wrapper">
<a href="<?php echo $factsheet_pdf['url']; ?>"><img src="/pdf-logo.png" /></a>
<span><?php echo $factsheet_title; ?></span>
</div>
</div>
</div>
<?php
if( get_row_index() % 4 == 0 ){
// Close the div and open it again
echo '</div><div class="vc_row wpb_row vc_row-fluid dt-default" style="margin-top: 0px; margin-bottom: 0px; min-height: 0px;">';
}
?>
<?php endwhile; ?>
</div>
<?php endif; ?>
James, thank you for the help!
This doesn’t produce exactly what I need, I have attached a screenshot from inspector that shows you what this produces so you can hopefully diagnose where the issue lies.
https://drive.google.com/file/d/0B970Ybn1DAYcRV9JS2pGbGt0Z0U/view?usp=sharing
As you can see it creates a new div to contain the columns but the div is without the classes that make it a row… Btw I added a / to the closing div as I assume that was needed?
EDIT: The opening div and closing divs you added in don’t seem to do anything, when I take them out it still works the same way as above?
Thanks James
Tom
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.