WordPress.org

Support

Support » How-To and Troubleshooting » Sorting posts by a custom field, if the custom filed exists

Sorting posts by a custom field, if the custom filed exists

  • Hi, I wonder if someone can help with the code below on Sorting posts (that exists in a specific category) by a custom field, but only if the custom filed exists, otherwise just have the posts (within the specific category) display as usual.

    In addition, all posts should wrap in <li>

    Here’s the code so far:

    <ul class="products">
    <?php
    global $post;
    $page_category = 4;
    $args = array('category'=>$page_category,'meta_key'=>'sorter','orderby'=>'meta_value_num','order' =>'ASC');
    $page_content = get_posts( $args );
    
    foreach( $page_content as $post ) : setup_postdata($post); ?>
    	<li><?php the_title(); the_content(); ?></a></li>
    <?php endforeach; ?>
    </ul>

    The code above will not show posts if the custom filed ‘sorter’ doesn’t exist.

    Any help would be very much appreciated!

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Sorting posts by a custom field, if the custom filed exists’ is closed to new replies.
Skip to toolbar