Custom Post Type order Title ASC

I´d like to display all car manufacturer in order by title (e.g. “Merecedes”) and order from lowest to highest values (e.g. “A-Class”) “ASC”, too. It should looks like a glossary.

Page: Cars

A

Read More

BMW:
1er
2er
3er

Mercedes:
A-Class
B-Class
C-Class

Z

This is my custom-port-type-cars.php

<h2>Mercedes</h2>
    <?php $args = array( 'post_type' => 'cars', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 

    // ============================= Mercedes ==============================
        'tax_query' => array(
            array(
                'taxonomy' => 'manufacturer',
                'field' => 'slug',
                'terms' => array('mercedes')
            )
        )
    );

    // ============================= OUTPUT ==============================
    $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post();
        the_title('<h3>', '</h3>');
        the_content();
    endwhile; ?>


<h2>BMW</h2> 
    <?php $args = array( 'post_type' => 'cars', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 

    // ============================= BMW ==============================
        'tax_query' => array(
            array(
                'taxonomy' => 'manufacturer',
                'field' => 'slug',
                'terms' => array('bmw')
            )
        )
    );


    // ============================= OUTPUT ==============================
    $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post();
        the_title('<h3>', '</h3>');
        the_content();
    endwhile; ?>

This doesn´t work for me. I can´t order title an terms at the same time.

Thanks for your help
Ogni

Related posts

Leave a Reply

1 comment

  1. $index = 'A';
    $terms = get_terms('manufacturer');
    
    foreach ($terms as $term) {
        if($index != strtoupper(substr($term->name, 0, 1))) {
            $index = strtoupper(substr($term->name, 0, 1));
            echo '<h1>'. $index . '</h1>';
        }
        ?>
        <h2><?php echo $term->name; ?></h2>
        <?php $args = array( 'post_type' => 'cars', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 
            'tax_query' => array(
                array(
                    'taxonomy' => 'manufacturer',
                    'field' => 'slug',
                    'terms' => array($term->slug)
                )
            )
        );
    
        // ============================= OUTPUT ==============================
        $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post();
            the_title('<h3>', '</h3>');
            the_content();
        endwhile;
    }