get posts by tag to showing in a widget
Question
Hi there dear developers! i make a widget in my wordpress theme to show 5 last video-based posts in that. and my video posts have a 'video' tag. how i can query that posts by tag to showing there? thanks.
Solution
You might want to go through:
https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
So using tag
parameter in WP_Query
, you can get posts tagged to 'video' tag.
Use orderby
and posts_per_page
to get last 5 video posts.
$query = new WP_Query( array( 'tag' => 'video', 'posts_per_page' => 5 ) );
while ($wp_query->have_posts()) : $wp_query->the_post();
//your code to display posts
endwhile;
Haven't tested the code, so watch out for typos.
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange