• I’m having trouble sorting post based on a custom category. I can sort all post alphbetically, but I want to sort post alphabetically based on the category they are affliated with.

    I have created five categories, AE, FJ, KO, PT, & UZ

    Basically I have a drop down, and want the user be able to select the category and be displayed alphabetically.

    <div id="sort">
    						<label>Sort by</label>
    						<form action="<?php bloginfo('url'); ?>/grants/all-grantees" method="get">
    							<select name='sort' onchange='return this.form.submit()'>">
    							<option>Please Choose</option>
    							<option value="D">Most Recent</option>
    							<option value="A">Name (Ascending)</option>
    							<option value="Z">Name (Descending)</option>
                                <option value="AE">A-E</option>
                                <option value="FJ">F-J</option>
                                <option value="KO">K-O</option>
                                <option value="UZ">U-Z</option>
    
    							</select>
    						</form>
    					<div class="clear"></div>
    					</div>
    
    					<?php
    					$temp = $wp_query;
    					$wp_query= null;
    					$wp_query = new WP_Query();
    
    					$sort = $_GET['sort'];
    					if ($sort==A) {		// sort A-Z
    						$wp_query->query('post_type=grants'.'&orderby=title'.'&order=ASC'.'&paged='.$paged);
    					} elseif ($sort==Z) {	// sort Z-A
    						$wp_query->query('post_type=grants'.'&orderby=title'.'&order=DESC'.'&paged='.$paged);
    				    } elseif ($sort==D) {	// sort chronological (Latest post to the Oldest Post)
    						$wp_query->query('post_type=grants'.'&orderby=date'.'&order=DESC'.'&paged='.$paged);
    					} elseif ($sort==FJ) {	// sort alphabetical (A - E)
    						$wp_query->query('post_type=grants'.'&orderby=title'.'&order=ASC'.'&paged='.$paged);
    					} else { 				//sort chronograntslogical (Latest post to the Oldest Post)
    						$wp_query->query('post_type=grants'.'&orderby=date'.'&order=DESC'.'&paged='.$paged);
    					}
    					?>
    
    					<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
Viewing 1 replies (of 1 total)
  • A little hard to tell from your code, but I think all you need to do is add the ‘&cat=nn’ parameter to each of the selected queries where ‘nn’ is the corresponding category id. If ’53’ is the id for the ‘AE’ category, the code would look like this:

    } elseif ($sort=='AE') {	// sort alphabetical (A - E)
       $wp_query->query('post_type=grants'.'&orderby=title'.'&order=ASC'.'&paged='.$paged.'&cat=53');
    } else {
Viewing 1 replies (of 1 total)
  • The topic ‘Sort Post by Category in Alphabetical Order’ is closed to new replies.