Support » Plugin: Custom Content Type Manager » How to order posts by custom field?

  • Resolved Becky Davis


    WCEU 2016 Contributor

    Hi, I’ve created a CPT for staff members and I’d like them to sort on the page by last name, so that no matter what order they’ve been entered in, they show up alphabetically.

    I have this code in the page template:
    ‘<?php query_posts( ‘post_type=staff’,’orderby=lname’, ‘order=ASC’); ?>’

    lname is the name of the custom field – but it just sorts by latest post on top. Is there something else I need to add, or? Please advise.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Becky Davis


    WCEU 2016 Contributor

    Ah got it. If there is a better way to do this, please let me know, but this does work. Assigning the custom field as a meta_key and writing the string with ampersands did the trick.

    ‘<?php query_posts( ‘post_type=staff&meta_key=lname&orderby=meta_value&order=ASC’); ?>’

    Plugin Contributor fireproofsocks


    Glad you got something that works. I hate query_posts() and WP_Query class… that’s why I wrote the GetPostsQuery class for the CCTM. You’d do something like this in your template file:

    $Q = new GetPostsQuery();
    $args = array();
    $args['post_type'] = 'staff';
    $args['orderby'] = 'lname';
    $args['order'] = 'ASC';
    $posts = $Q->get_posts($args);
    foreach ($posts as $p) {
    // print stuff here, e.g. $p['post_title'];


    Becky Davis


    WCEU 2016 Contributor

    Thank you for this and please forgive my lack of PHP knowledge, but your query array is not working. I suspect that I need to re-write how custom fields are printing, but I’m unclear on how to do that, especially since I need the fields to be wrapped in divs.

    This is what the whole chunk looks like:

    <?php query_posts( 'post_type=staff&meta_key=lname&orderby=meta_value&order=ASC'); ?>
                    <?php while ( have_posts() ) : the_post(); ?>
    		<div class="red-left-photo"><img src="<?php print_custom_field('headshot:to_image_src'); ?>"/></div>
    		<div class="red-left-box"><p class="staff-title"><?php print_custom_field('fname'); ?>
            <?php print_custom_field('lname'); ?> - <?php print_custom_field('title'); ?></p>
    		<?php print_custom_field('education'); ?>
            <span class="red">___</span>
            <?php print_custom_field('education2'); ?>
            <span class="red">___</span>
    		<?php print_custom_field('education3'); ?>
    <?php endwhile; // end of the loop. ?>

    If I replace the top line string with your array, then nothing prints. How can I re-write this? Thanks for your help and for this awesome plugin.

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your posted code may now have been permanently damaged/corrupted by the forum’s parser.]

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to order posts by custom field?’ is closed to new replies.