admin管理员组文章数量:1205154
I'd like to display a list of products for a certain category, sorted by the post_excerpt. The original code I used was this:
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'my-product-category' ),
'operator' => 'IN'
)
),
'orderby' => 'post_excerpt',
'order' => 'ASC'
);
$loop = new WP_Query( $args );
This displays the products, but the orderby is ignored in a taxonomy query. So I've changed the query to:
$args = array(
'post_type' => 'product',
'orderby' => 'post_excerpt',
'order' => 'ASC',
'product_cat' => 'My Product Category',
'post_status' => 'publish'
);
$loop = new WP_Query( $args );
The products are displayed, but the orderby clause is still ignored.
I'd like to display a list of products for a certain category, sorted by the post_excerpt. The original code I used was this:
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'my-product-category' ),
'operator' => 'IN'
)
),
'orderby' => 'post_excerpt',
'order' => 'ASC'
);
$loop = new WP_Query( $args );
This displays the products, but the orderby is ignored in a taxonomy query. So I've changed the query to:
$args = array(
'post_type' => 'product',
'orderby' => 'post_excerpt',
'order' => 'ASC',
'product_cat' => 'My Product Category',
'post_status' => 'publish'
);
$loop = new WP_Query( $args );
The products are displayed, but the orderby clause is still ignored.
Share Improve this question asked Aug 7, 2012 at 20:54 JonathanJonathan 211 silver badge2 bronze badges 1 |2 Answers
Reset to default 1If you take a look at the WordPress Codex 'post_excerpt' is not a valid parameter for the 'orderby' parameter
For this reason it cannot be done through WP_Query(). If you don't mind, why are you trying to sort by post_excerpt in the first place?
Now, you can use filter posts_orderby.
add_filter('posts_orderby', function($orderby) {
return str_replace(
'wp_posts.post_modified',
'wp_posts.post_excerpt',
$orderby);
}, 10, 3);
$custom['orderby'] = 'modified';
new WP_Query( $custom );
本文标签: categoriesDisplay products for a categorysorted by postexcerpt
版权声明:本文标题:categories - Display products for a category, sorted by post_excerpt 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738699290a2107566.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
[orderby] => post_excerpt
doesn't show up? – pcarvalho Commented Aug 8, 2012 at 14:34