I have a custom post type - Demo Tours
And for that CPT I created a Custom Taxonomy - Demo Tour Categories. Each Demo Tour may have one or more category. Each category has Custom Field - Category Image/Video
On front end, the query gets all the custom taxonomy entries (Tour Categories) display the category Image and Description on the left, and the Demo Tours that belong to that category listed on the right. Pretty basic and works like a charm.
The problem is, I must control the list order of Demo categories (and the demo tours that belong to them). Now the orderby is by their ID.
But I want to add a Custom field - Order Number to Custom Taxonomy - Demo Categories, and on front end I want to display them depending on the Order Number.
Custom Fields created with ACF plugin and the Custom Post Types created with Pods Admin plugin.
I have really spent a significant time on web to find a solution but nothing matches exactly what I need.
I believe the problem is my approach but can't really put my finger on the problem.
Please show me a way :)
Here is my code : (briefly)
first I get the terms :
$terms = get_terms(
array(
'taxonomy' => 'tour_category',
'hide_empty' => true,
)
);
then I loop them to show a header with Category names (like a menu)
foreach($terms as $term) {
echo ' <a class="demo_cat_link" href="#' . $term->slug . '">' . $term->name . '<li class="demo_cat"></li></a>';
}
then I display the categories on the left
$i = 0;
foreach ($terms as $terms => $term) {
$i++ != 0 ? $fClass = "fade" : $fClass = "" ;
$cat_id = $term->term_id;
$cat_video = get_field('featured_video', $term->taxonomy . '_' . $term->term_id );
$cat_order = get_field('tour_category_list_order', $term->taxonomy . '_' . $term->term_id );
<div class="loop_left_section">
<div class="tour_cat_thumb <?=$fClass?>">
<video class="demo_featured" width="620" autoplay="autoplay" loop="loop" muted="">
<source type="video/mp4" src="<?php echo $cat_video; ?>">
</video>
</div> <? // tour_cat_thumb ?>
<h2><?php echo $term->name; ?></h2>
<p><?php echo $term->description; ?></p>
</div> <? // loop_left_section ?>
and the demo tours on the right
<div class="loop_right_section">
<?php
$args = array(
'post_type' => 'demotours',
'tax_query' => array(
array(
'taxonomy' => 'tour_category',
'field' => 'slug',
'terms' => $term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
while($loop->have_posts()) : $loop->the_post();
$demo_tour_link = ( get_field('demo_tour_link', get_the_ID() ) ? get_field('demo_tour_link', get_the_ID() ) : "#" );
echo '<a href="'.$demo_tour_link.'" class="tour_link">
<div class="demo_tour_wrap">
<h3>' . get_the_title() . '</h3>
<p>'. get_the_excerpt() . '</p>
</div>
</a>';
endwhile;
}
?>
</div> <? // loop_right_section ?>
$terms
array? You'd have to do this immediately after yourget_terms
call, using the standard PHP array sorting functions, and you'll probably need a sorting function to check the term meta with the order value you've added ( custom fields === meta, meta is the internal/real name )