最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

query - Order posts ascending from posts in array

programmeradmin1浏览0评论

I have all my post id's in a specific array $productIds. When I want to show only these posts, I do this query:

$args = array( 
        'post_type' => 'Product',
        'orderby' => "post__in",
        'posts_per_page' => -1,
        'post__in' => $productIds
        );

But this shows all the posts ordered from Z to A. So I've added an array in the orderby:

 $args = array( 
        'post_type' => 'Product',
        'orderby' => array (
          'post__in' => $productIds,
          'title' => 'ASC'
        ),
        'posts_per_page' => -1
        );

All the posts are shown, so not only the ones that are in the array $productIds

How do I order alphabetically (A->Z) and only show the ones that are in the array?

I have all my post id's in a specific array $productIds. When I want to show only these posts, I do this query:

$args = array( 
        'post_type' => 'Product',
        'orderby' => "post__in",
        'posts_per_page' => -1,
        'post__in' => $productIds
        );

But this shows all the posts ordered from Z to A. So I've added an array in the orderby:

 $args = array( 
        'post_type' => 'Product',
        'orderby' => array (
          'post__in' => $productIds,
          'title' => 'ASC'
        ),
        'posts_per_page' => -1
        );

All the posts are shown, so not only the ones that are in the array $productIds

How do I order alphabetically (A->Z) and only show the ones that are in the array?

Share Improve this question asked Feb 25, 2021 at 11:15 DennisDennis 1358 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can try this

$args = array( 
    'post_type' => 'Product',
    'orderby' => "name",
    'order'  => 'ASC',
    'posts_per_page' => -1,
    'post__in' => $productIds
);
发布评论

评论列表(0)

  1. 暂无评论