• I have a custom post type called staff. I created a shortcode to list the entries, following this tutorial: https://code.tutsplus.com/tutorials/create-a-shortcode-to-list-posts-with-multiple-parameters–wp-32199.

    My working shortcode sorts by the post title and it works fine. But sometimes I need to sort by something else, and would like to sort by “job_name” one of the fields in the staff custom post type.

    I have googled and tried various combinations of

    'orderby' => 'meta_value',
    'meta_key' => 'job_name',

    and

    'orderby' => 'meta_value',
        'meta_query' => array(
            array(
                'key' => 'job_name',
                'compare' => 'LIKE'
            )
        )
     );

    but can’t get it to work. Obviously I am not a coder. I would appreciate any help. Thanks very much.

    Shortcode I am using that sorts by post title is below.

    add_shortcode( 'vma-list-posts', 'vma_post_listing_parameters_shortcode' );
    function vma_post_listing_parameters_shortcode( $atts ) {
        ob_start();
    
        // define attributes and their defaults
        extract( shortcode_atts( array (
            'type' => 'staff',
            'order' => 'ASC',
            'orderby' => 'title',
            'posts' => -1,
            'position_type' => '',
            'department' => '',
    //        'category' => '',
        ), $atts ) );
    
        // define query parameters based on attributes
        $options = array(
            'post_type' => $type,
            'order' => $order,
            'orderby' => $orderby,
            'posts_per_page' => $posts,
            'position_type' => $position_type,
            'department' => $department,
      //      'category_name' => $category,
        );
        $query = new WP_Query( $options );
        // run the loop based on the query
        if ( $query->have_posts() ) { ?>
         <ul class="large-block-grid-4 small-block-grid-1 medium-block-grid-2">
    	<?php while ( $query->have_posts() ) : $query->the_post(); ?>
                 <li>
    		 <?php the_post_thumbnail('vma-staff'); ?>
                       <?php
    			//	if(get_field('first_name'))
    			{
    			//echo '<img src="' . get_field('staff_photo') . '" />';
    			echo '<p class="staffname">' . get_field('staff_title') . ' '. get_field('first_name') . ' ' . get_field('last_name') . '</p>';}
    			if(get_field('job_title'))
    			{
    			echo '<p class="staffjobtitle">' . get_field('job_title') . '</p>';
    			}
    			{
    			echo '<p class="staffemail">' . '<a href="mailto:' . get_field('email') . '">' . get_field('email') . '</a></p>';
    			echo '<p class="staffphoto">Ext. ' . get_field('extension') . '</p>';
    			}
     			?>
    		  <?php the_content(); ?>
             </li>
    			<?php endwhile;
    			wp_reset_postdata(); ?>
    		</ul>
        <?php
            $myvariable = ob_get_clean();
            return $myvariable;
        }
    }
  • The topic ‘Shortcode that orders custom post types by a custom field’ is closed to new replies.