2

I created custom post types and a taxonomy-adhesion.php file (by copying the contents of the archive.php file).

I wish the search result would appear in alphabetical order (or by title), but it does not.

Here is the code of the page:

<?php get_header(); ?>

<div class="content">

    <div class="page-title">

        <h4>

            <?php if ( is_day() ) : ?>
                <?php _e('Date', 'wilson'); ?><span class="name"><?php echo get_the_date(); ?></span>
            <?php elseif ( is_month() ) : ?>
                <?php _e('Month', 'wilson'); ?><span class="name"><?php echo get_the_date('F Y'); ?></span>
            <?php elseif ( is_year() ) : ?>
                <?php _e('Year', 'wilson'); ?><span class="name"><?php echo get_the_date('Y'); ?></span>
            <?php elseif ( is_category() ) : ?>
                <?php _e('Category', 'wilson'); ?><span class="name"><?php echo single_cat_title( '', false ); ?></span>
            <?php elseif ( is_tag() ) : ?>
                <?php _e('Tag', 'wilson'); ?><span class="name"><?php echo single_tag_title( '', false ); ?></span>
            <?php elseif ( is_author() ) : ?>
                <?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); ?>
                <?php _e('Author', 'wilson'); ?><span class="name"><?php echo ($curauth->display_name); ?></span>
            <?php else : ?>
                <?php _e( 'Les Membres', 'wilson' ); ?>
            <?php endif; ?>

        </h4>

        <?php
            $tag_description = tag_description();
            if ( ! empty( $tag_description ) )
                echo apply_filters( 'tag_archive_meta', $tag_description );
        ?>

    </div> <!-- /page-title -->

    <div class="posts">

        <?php if ( have_posts() ) : ?>

            <?php rewind_posts(); ?>

            <?php while ( have_posts() ) : the_post(); ?>

                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

                    <?php get_template_part( 'contentmembres', get_post_format() ); ?>

                </div> <!-- /post -->

            <?php endwhile; ?>

    </div> <!-- /posts -->

    <?php if ( $wp_query->max_num_pages > 1 ) : ?>

        <div class="archive-nav">

            <?php echo get_next_posts_link( __('Older<span> posts</span>', 'wilson')); ?>

            <?php echo get_previous_posts_link( __('Newer<span> posts</span>', 'wilson')); ?>

            <div class="clear"></div>

        </div> <!-- /post-nav archive-nav -->

    <?php endif; ?>

<?php endif; ?>

<?php get_footer(); ?>  
6

The main query is generated before the template is loaded, the results of the main query are how WordPress knows what template to load. If you want to alter query parameters of the main query to change things like orderby, you should add a function hooked to pre_get_posts.

The argument passed to the function contains the query parameters of each query, so you can check which query it is and only alter your taxonomy main query. This function would go in your theme's functions.php file, or a plugin.

function wpd_adhesion_taxonomy_queries( $query ) {
    if ( !is_admin() && $query->is_tax( 'adhesion' ) && $query->is_main_query() ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'wpd_adhesion_taxonomy_queries' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.