0
votes

I am fighting to get what should be a very simple Wordpress custom taxonomy archive template to work.

I'm getting all the information and the posts are listed alphabetically but the terms are in order of id and need them in alphabetical order.

I'm fudging my way through this and am currently using the code from this post. I have tried a multitude of solutions from around the net, with no luck. I see this post has a solution but don't know how to implement it in the code below.

Perhaps there's an easier way of doing what I need?

The query needs to get the current parent term, then the child terms, and the posts in the child terms. The following code is on my taxonomy-business-categories-(parent term).php, for example my taxonomy-business-categories-bars.php. I need to output the child terms grouped with their posts. All must be in alphabetical order.

<?php 
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
    $wpq = array (
    'taxonomy'=>$taxonomyName,
    'term'=>$term->slug,
    'order'=>'asc',
    'orderby'=>'title');
    $query = new WP_Query ($wpq);
    echo "$term->name:<br />"; 
    ?>

    <?php
    if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
    <a href="<?php the_permalink();?>"><?php the_title();?></a>, 
    <?php endwhile; endif; wp_reset_query(); ?>
    <?php
    echo "<br />";   
 }
?>

Here's a link to the taxonomy template as .txt. file.

UPDATE: As I'm hardcoding the taxonomy templates by parent term, could I use something like this with my code above:

<?php 
$term_id = 32;
$taxonomy_name = 'business-categories';
$termchildren = get_term_children( $term_id, $taxonomy_name );
$children = array();
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomy_name );
$children[$term->name] = $term;
}
ksort($children);
2

2 Answers

0
votes

Replace this code with your above provided code

<?php 
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'name', $child, $taxonomyName );
$wpq = array (
'taxonomy'=>$taxonomyName,
'term'=>$term->slug,
'order'=>'asc',
'orderby'=>'title');
$query = new WP_Query ($wpq);
echo "$term->name:<br />"; 
?>
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
<a href="<?php the_permalink();?>"><?php the_title();?></a>, 
<?php endwhile; endif; wp_reset_query(); ?>
<?php
echo "<br />";   
}
?>

Let me know if that works for you ...

0
votes

The solution in my case was to install the plugin Custom Taxonomy Order NE and (as directed by the plugin's author) adjusted the query as follows:

Replace this:

$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
$wpq = array (

With this

$termchildren = get_terms( array(
'taxonomy' => $taxonomyName,
'child_of' => $current_term->term_id,

) );

foreach ($termchildren as $term) {
$wpq = array (