I've been given some code to tidy up and edit a bit and the client wants to organize a custom post type named "plans" alphabetically.

It's not a straight forward query and I've never come across anything like this before, any ideas?

<?php $planIDs = get_post_meta($post->ID, '_lm_comm_plans', false); ?>
<?php foreach ($planIDs as $planID) : ?>
    <?php $pdfID = get_post_meta($planID, '_lm_plan_pdf', true); ?>
        <?php $pdfURL = wp_get_attachment_url($pdfID); ?>
            <?php $planTitle = get_the_title($planID); ?>
                <li class="plan"><!-- content here --></li>
<?php endforeach; ?>
  • where does $planIDs come from?
    – Milo
    Commented Jan 6, 2012 at 19:40
  • My mistake, I missed a line out. Edited. Commented Jan 6, 2012 at 19:52

1 Answer 1


You'll need to run through get_posts() to apply the order by handling.

$planIDs = get_post_meta( $post->ID, '_lm_comm_plans', false );
foreach( get_posts( array( 'post__in' => $planIDs, 'orderby' => 'name', 'order' => 'ASC', 'post_type' => 'plans' ) ) as $plan ) {
    $pdfID = get_post_meta( $planID, '_lm_plan_pdf', true );
    $pdfURL = wp_get_attachment_url( $pdfID );
    $planTitle = get_the_title( $planID );
    <li class="plan"><!-- content here --></li>
  • That didn't seem to work, the page is completely blank. Commented Jan 9, 2012 at 9:58
  • Dean, Sorry, I didn't pay attention the fact that the related plans were another post type. You just need to make sure the post type is set in the query. I've updated the code for this. Commented Jan 9, 2012 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.