WordPress.org

Ready to get started?Download WordPress

Forums

Custom Content Type Manager
[resolved] How to order posts by custom field? (4 posts)

  1. beckyddesign
    Member
    Posted 1 year ago #

    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.
    Thanks

    http://wordpress.org/extend/plugins/custom-content-type-manager/

  2. beckyddesign
    Member
    Posted 1 year ago #

    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'); ?>'

  3. fireproofsocks
    Member
    Plugin Author

    Posted 1 year ago #

    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:

    <?php
    $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'];
    }
    ?>

    See http://code.google.com/p/wordpress-custom-content-type-manager/wiki/get_posts_examples

  4. beckyddesign
    Member
    Posted 1 year ago #

    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'); ?>
            </div>
    
    <?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.]

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic