admin管理员组文章数量:1315286
I'm trying to get the latest 4 posts filtered by category. My post type are products.
I already tried wp_get_recent_posts, but it doesn't have an attribute for category filter.
Then i tried with the WP_Query, but is not working too.
$args2 = array(
'post_type' => 'product',
'taxonomy' => 'product_cat',
'category' => '30278'
);
$the_query = new WP_Query($args2);
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
It shows me always the same posts.
This is my recent post code:
$args = array('post_type' => 'product',
'numberposts' => 4,
'include' => get_cat_ID($atts['category']),
);
wp_get_recent_posts($args, $atts['category']);
I tried to add in my args include, exclude, category and category name. without results. I really don't know how to solve this. Thank you in advance.
I'm trying to get the latest 4 posts filtered by category. My post type are products.
I already tried wp_get_recent_posts, but it doesn't have an attribute for category filter.
Then i tried with the WP_Query, but is not working too.
$args2 = array(
'post_type' => 'product',
'taxonomy' => 'product_cat',
'category' => '30278'
);
$the_query = new WP_Query($args2);
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
It shows me always the same posts.
This is my recent post code:
$args = array('post_type' => 'product',
'numberposts' => 4,
'include' => get_cat_ID($atts['category']),
);
wp_get_recent_posts($args, $atts['category']);
I tried to add in my args include, exclude, category and category name. without results. I really don't know how to solve this. Thank you in advance.
Share Improve this question edited Apr 4, 2018 at 20:47 Krzysiek Dróżdż 25.5k9 gold badges53 silver badges74 bronze badges asked Apr 4, 2018 at 19:59 KryukoKryuko 514 bronze badges 1 |1 Answer
Reset to default 1There should be no difference and either custom WP_Query
or wp_get_recent_posts
should work like a charm in this case. (To be honest, wp_get_recent_posts
uses get_posts and this one is based on
WP_Query`).
So the problem is not with methods you're trying to use, but the way you use them...
wp_get_recent_posts
wp_get_recent_posts
function takes two arguments:
- $args - list of arguments that describe the posts you want to get,
- $output - constant OBJECT, ARRAY_A which describes how the result will be formatted.
So let's take a look at your call of that function:
$args = array('post_type' => 'product',
'numberposts' => 4,
'include' => get_cat_ID($atts['category']),
);
wp_get_recent_posts($args, $atts['category']);
You put $atts['category']
as second argument, so the $output
argument is incorrect. And even worse - you put category ID as include
argument, which should be a list of posts that should be included...
How to make it work?
$args = array(
'post_type' => 'product',
'numberposts' => 4,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => 30278,
'operator' => 'IN'
)
) );
$recent_posts = wp_get_recent_posts( $args );
WP_Query
So why the WP_Query method doesn't work?
Take a look at possible parameters of WP_Query: https://codex.wordpress/Class_Reference/WP_Query
There is no parameter called taxonomy
, so this one will be ignored.
There is also no category
param, so this one will be ignored too.
And how to make it work? Easy - just use the same $args
as in wp_get_recent_posts
call above.
本文标签: wp queryGet latest 4 post on a custom post filtered by category
版权声明:本文标题:wp query - Get latest 4 post on a custom post filtered by category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741979469a2408309.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
product_cat
you need to run a taxonomy query. Thecategory
isn't working because aproduct_cat
isn't technically acategory
. – WebElaine Commented Apr 4, 2018 at 20:05