• Resolved Sleneau

    (@sleneau)


    Hi, as the title states, I am having trouble with the get_posts() function. I have the following set of code:

    $args = array(
    		'posts_per_page'   => -1,
    		'offset'           => 0,
    		'Series'		   => $ser,
    		'post_type'        => 'interviews',
    		'meta_key'		   => 'position',
    		'orderby'          => 'meta_value_num',
    		'order'            => 'DESC',
    		'post_status'      => 'publish'		); 
    
    	$posts_array = get_posts($args);

    This calls up an array of posts from the interviews custom post type, which are in a custom taxonomy called ‘Series’, and which are supposed to be ordered by a numeric custom field named “position”. All of the taxonomies, post types, and meta field names are spelled correctly. I quintuple-checked.

    In short, it isn’t working. The posts are being called and displayed, but in the wrong order. If it’s any help, I use the Advanced Custom Fields plugin, and the code I am using is very similar to what they suggest for integrating their plugin with get_posts.

    I doubt that plugin is the problem. Any help anybody can offer would be greatly appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • I think the taxonomy needs to be specified by its slug, not its name. Try using 'series' => $ser,

    Thread Starter Sleneau

    (@sleneau)

    You’re right. Once I changed it to the slug, all my issues disappeared. It’s so weird though, since the documentation doesn’t specify that you have to feed it the post slug, and the actual post name works half the time.

    Thanks for the information, much appreciated.

    Thread Starter Sleneau

    (@sleneau)

    Oh, and just to be clear, the slug had to be passed to a field that is the same name as my taxonomy. Which, in this case, is ‘Series’. So, the resulting code would look something like what follows:

    'series' => 'SLUG-OF-TAXONOMY-TERM'

    This is, of course, passed to the get_posts() in the form of an argument array. I thought it might be confusing since I use an unexplained variable in my example above.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get_posts isn't sorting posts by the meta_key I provided.’ is closed to new replies.