I have created a custom post type called "al_product" and attached to it a custom taxonomy called "product_category".
In my archive-product_category.php I have this (relevant) code:
if ( $termchildren ) {
// we have kids...just show the terms.
$i=1;
foreach ( $termchildren as $child ) {
if ($i % 2 == 0) {
echo '<div class="tax-entry flex">';
} else {
echo '<div class="tax-entry flex" style="flex-direction: row-reverse;">';
}
?>
<figure class="wp-block-media-text__media">
<?php if ( get_field('product_category_img', $child) ) echo wp_get_attachment_image( get_field('product_category_img', $child), 'full' ) ;?>
</figure>
<div style="flex:60% 0 0;" class="wp-block-media-text__content">
<h2><?php echo $child->name ;?></h2>
<p>
<?php
if (get_field('short_description', $child )){
echo get_field('short_description', $child );
}else{
echo $child->description;
}?>
</p>
<?php
// Get (at most) 2 "product" posts in the child term.
$posts = get_posts( array(
'post_type' => 'al_product',
'posts_per_page' => 2,
$taxonomy_name => $child->slug,
) );
// If there's exactly 1 post, use the post permalink.
if ( 1 === count( $posts ) ) : ?>
<a class="button" href="<?php the_permalink( $posts[0] ); ?>">See this Product</a>
<?php // Else, use the term link.
else : ?>
<a class="button" href="<?php echo esc_url( get_term_link( $child, $taxonomy_name ) ); ?>">See this Series</a>
<?php endif; ?>
</div>
</div> <!-- END FLEX -->
<?php
$i++;
}
} else {
// no kids...show the products
echo '<div class="productCategories grid">';
while ( have_posts() ) : the_post();
?>
<div class="product_cat">
<a href="<?php the_permalink();?>">
<?php the_post_thumbnail('small');?>
<h2><?php the_title();?></h2>
<?php
$subtitle = get_field('subtitle');
if ( $subtitle ) {
echo '<h3 class="entry-subtitle">'.$subtitle.'</h3>';
}
?>
</a>
<?php //if (get_field('specs')['part_number']) echo '<span>'.get_field('specs')['part_number'].'</span>';?>
</div>
<?php
endwhile;
echo '</div>';
}
The section I'm questioning is when there are no kids so we show products.
Currently this is returning the products correctly but sorting by title. I would like to sort by menu_order.
I added this to my functions.php:
add_action( 'pre_get_posts', 'rt_change_product_sort_order');
function rt_change_product_sort_order($query){
if(get_query_var( 'product_category' )):
//Set the order ASC or DESC
$query->set( 'order', 'DESC' );
//Set the orderby
$query->set( 'orderby', 'menu_order' );
endif;
};
To no avail. I've also tried replacing the "If" statement with each of these:
if(is_post_type_archive( 'product_category' )):
if(get_query_var( 'product_category' )):
if(get_query_var( 'al_product' )):
What am I missing here?
is_tax()
. If it's the post type archive page, then the post type would need to have thehas_archive
set to true, then useis_post_type_archive( 'al_product' )
. Is archive enabled for your CPT?if(is_tax( 'product_category' )):
If you post as an answer. I'll check it! Thanks. @SallyCJ