• nicolasno

    (@nicolasno)


    Alert newbie ??

    I’ve created a archive to make a member list (with custom post type), but i want to sort post by alphabetic order.

    i suppose that i’ve to make a query order by, but i’m just a newbie… ??

    Could you help me ?

    Thank you

    This is the code of my 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(); ?>

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You mainly need to hook the ‘pre_get_posts‘ action to set the ‘orderby’ query var to ‘title’ (assuming that’s the alpha sorting field you wanted). The examples should help you get started.

    Your main problem besides simply writing the hook code will be preventing your code from being run for all queries. The examples show some conditions, like is_home and is_main_query. You will want the is_main_query part, but I’m not sure what else you will need. You’ll need something.

    For example, if these are for a custom post type ‘member’, your conditional could look like this:
    if ( $query->is_main_query() && 'member' == $query->get('post_type')) {

    You will need to identify something in the query that differentiates this alpha sorted query from all others. The sample query object at the end of the linked article lists all the various query vars available. At least one of those is going to be unique for this page, you just need to identify it.

Viewing 1 replies (of 1 total)
  • The topic ‘how to sort by alpha in archive.php’ is closed to new replies.