Could someone please help me to add category id with in this query so this will return posts from a certain category? Thanks in advance.
<?php foreach(_get($wp_query->posts? : array()) as $n => $post){ setup_postdata($post); ?>
// HTML and post Tags
<?php } ?>
Please follow proper wordpress method not follow someone’s code or copy paste code.
/*
* CODEX: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
* Source: https://core.trac.wordpress.org/browser/tags/3.9/src/wp-
includes/query.php
*/
$args = array(
//////Category Parameters - Show posts associated with certain categories.
//http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
//all categories parameter
'cat' => 5,//(int) - use category id.
'category_name' => 'staff, news', //(string) - Display posts that
have these categories, using category slug.
'category_name' => 'staff+news', //(string) - Display posts that
have "all" of these categories, using category slug.
'category__and' => array( 2, 6 ), //(array) - use category id.
'category__in' => array( 2, 6 ), //(array) - use category id.
'category__not_in' => array( 2, 6 ), //(array) - use category id.
'post_type' => 'post', //post or you can use your custom post type
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date');
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// Do Stuff
} // end while
} // endif
// Reset Post Data
wp_reset_postdata();