0

Our website was built by a programmer who is no longer available to assist us. I received enough of a tutorial from him to pretty much maintain our website.

He created a custom template that (posts1.php) that alphabetizes posts within a particular category. However, the template only has one parameter (Last Name) and we would like to further sort the posts by first name.

Below is the complete content of the template. Could you let me know how to edit the template to allow a the website to sort the posts by last name followed by first name? Thank you.

<?php
/*
Template Name Posts: posts1
*/
?>
<?php get_header(); ?>

    <div id="mainCol">

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

        <p class="title"><?php
    // Get the ID of a given category
    $category_id = get_cat_ID( 'Enshrined Officers' );

    // Get the URL of this category
    $category_link = get_category_link( $category_id );
?>

<!-- Print a link to this category -->
<a href="<?php echo $category_link; ?>" title="Enshrined Officers"><span class="back">&larr; Return To List</span></a>

<?php next_post_link_plus( array(
                    'order_by' => 'custom',
                    'meta_key' => 'Last Name',
                    'loop' => false,
                    'end_post' => false,
                    'thumb' => false,
                    'max_length' => 0,
                    'format' => '%link',
                    'link' => '<span class="enshr_next">Next &raquo;</span>',
                    'tooltip' => '',
                    'in_same_cat' => true,
                    'num_results' => 1,
                    'echo' => true
                    ) ); ?>

<?php previous_post_link_plus( array(
                    'order_by' => 'custom',
                    'meta_key' => 'Last Name',
                    'loop' => false,
                    'end_post' => false,
                    'thumb' => false,
                    'max_length' => 0,
                    'format' => '%link',
                    'link' => '<span class="enshr_prev">&laquo; Previous |</span>',
                    'tooltip' => '',
                    'in_same_cat' => true,
                    'num_results' => 1,
                    'echo' => true
                    ) ); ?></p>
            <div class="post" id="post-<?php the_ID(); ?>">
                <h1><?php the_title(); ?></h1>

                <div class="entry">
                    <?php the_content('Read the rest of this entry &raquo;'); ?>
                    <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
            <br />
                    <div class="back"<?php
    // Get the ID of a given category
    $category_id = get_cat_ID( 'Enshrined Officers' );

    // Get the URL of this category
    $category_link = get_category_link( $category_id );
?>

<!-- Print a link to this category -->
<a href="<?php echo $category_link; ?>" title="Enshrined Officers"><span class="back">&larr;<span> Return To List</a>
          </div>
            <br />
                  <?php the_tags('<div class="tags"><p><strong>Tags:</strong> ', ' , ' , '</p></div>'); ?>
                </div>
            </div>



    <?php endwhile; else: ?>
        <h2 class="center">Oops...</h2>
        <p class="center">Sorry, no posts matched your criteria.</p>
        <?php include (TEMPLATEPATH . "/searchform.php"); ?>

    <?php endif; ?>

    </div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>
  • It looks like you have some formatting issues with he code you posted. It will be hard to help you unless you pasted the code properly. – Matthew Woodard Feb 15 '18 at 20:17
  • This code does not alphabetise any posts as far as I can see. It looks only to link to the next/previous post (with the use of a plugin) which might be alphabetised (I don't know the plugin). You need to find where this query is defined and then edit the sorting there. Then you can use the new sorting clause: make.wordpress.org/core/2014/08/29/… – Beee Feb 15 '18 at 21:42
  • the code does sort.. and by last name... if you are considering they are on single pages and the previous and next links are going to the previous or next page based on last name. – rudtek Feb 15 '18 at 22:40
  • What are the next_post_link_plus and previous functions? – Tom J Nowell Feb 15 '18 at 22:57
1

can you try changing these lines:

'order_by' => 'custom',
'meta_key' => 'Last Name',

in each of the above sections with this code:

'meta_query' => array(
    'relation' => 'AND',
    'last_name_sort' => array(
        'key'     => 'last_name',
        'compare' => 'EXISTS',
    ),
    'first_name_sort' => array(
        'key'     => 'first_name',
        'compare' => 'EXISTS',
    ), 
),
'orderby' => array(
    'last_name_sort' => 'ASC',
    'first_name_sort' => 'ASC',
),

(this is assuming that your first name key is 'first_name')

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.