I'm trying to order this roster by Jersey Number but haven't been able to make it work. I'm not a programmer and have searched and tried to make this work multiple times.
Thank you so much for any help.
http://onramp.website/rosters-2017/17u-navy/
Here is the PHP code with the output:
<?php get_header(); ?>
<?php wp_reset_query(); ?>
<?php
$post_name = $post->post_name;
if ($post_name == "17u-navy") {
$post_type = "17u_navy";
}
elseif ($post_name == "15u-navy") {
$post_type = "15u_navy";
}
elseif ($post_name == "17u-red") {
$post_type = "17u_red";
}
elseif ($post_name == "17u-white") {
$post_type = "17u_white";
}
elseif ($post_name == "17u-gold") {
$post_type = "17u_gold";
}
$args = array('post_type'=>$post_type,'posts_per_page'=>-1, 'orderby' => 'number', 'order' => 'ASC',);
$the_query = new WP_Query( $args );
$flag = 0;
if ($the_query->have_posts()):
?>
<?php while ($the_query->have_posts()): $the_query->the_post(); ?>
<?php
$flag++;
?>
<?php if ($flag == 999): ?>
<?php $flag = 0; ?>
<?php endif; ?>
<h3 class="h-custom-headline cs-ta-left h4 accent"><span><?php the_title(); ?> #<?php the_field('number'); ?></span></h3>
<div id="x-section-2" class="x-section" style="margin: 0px 0px 25px 0px;padding: 0px; background-color: transparent; margin-bottom: 50px;">
<div class="x-column x-sm x-1-5" style="padding: 0px;">
<img class="x-img x-img-none" src="<?php the_field('profilephoto'); ?>">
</div>
<div class="x-column x-sm x-4-5" style="padding: 0px;">
<ul class="x-block-grid two-up">
<li class="x-block-grid-item"><strong>Class of <?php the_field('gradyear'); ?></strong><br />
<strong>Height:</strong> <?php the_field('height'); ?><br />
<strong>Position:</strong> <?php the_field('position'); ?><br />
<?php if( get_field('bballhonors') ): ?>
<strong>Basketball Honors:</strong> <?php the_field('bballhonors'); ?><br />
<?php endif; ?>
<?php if( get_field('ncaaclearing') ): ?>
<strong>NCAA Clearing House:</strong> <?php the_field('ncaaclearing'); ?><br />
<?php endif; ?>
<?php if( get_field('highlightfilm') ): ?>
<strong>Highlight Film:</strong> <a href="<?php the_field('highlightfilm'); ?>" target="_blank">link</a>
<?php endif; ?>
<?php if( get_field('hobbies') ): ?>
<strong>Hobbies:</strong> <?php the_field('hobbies'); ?>
<?php endif; ?>
</li>
<li class="x-block-grid-item">
<strong>High School:</strong> <?php the_field('highschool'); ?><br />
<strong>Hometown:</strong> <?php the_field('citystate'); ?><br />
<?php if( get_field('gpa') ): ?>
<strong>GPA:</strong> <?php the_field('gpa'); ?><br />
<?php endif; ?>
<?php if( get_field('sat') ): ?>
<strong>SAT:</strong> <?php the_field('sat'); ?><br />
<?php endif; ?>
<?php if( get_field('schoolhonors') ): ?>
<strong>School Honors:</strong> <?php the_field('schoolhonors'); ?><br />
<?php endif; ?>
<?php if( get_field('favoritequote') ): ?>
<strong>Favorite Quote:</strong><em><br />"<?php the_field('favoritequote'); ?>"</em><br />
<?php if( get_field('author') ): ?>
~ <?php the_field('author'); ?>
<?php endif; ?><?php endif; ?></li>
</ul>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
What you're trying to do seems simple, and with the right tools is very simple, but trying to do something like extract the Jersey Number and then order the posts by that is going to leave a mess of a WP_Query and is just a horrible idea.
There's a few ways to 'cheat' in terms of post order.
You could get a plugin that allows you to create a custom post order, but this breaks the ordering via WP_Query and 'locks' it, so I always avoid that when I can.
You could get a plugin called 'Advanced Custom Fields' and create a new field where you will write Jersey number of the post, then order the query by that field, which is probably the best way to do it.
I also like to create an unlimited repeater field that will allow me to choose a single post per repeated field and keep adding posts one by one in whatever order I want - this changes the way you structure the query and the template so it's a little bit more complex.
(also that chain of ifs at the beginning is horrible to look at. It's going to lead to pain in terms of management later on. I wish I could just write the template and test it :( )