lurning too koad
lurning too koad

Reputation: 2964

PHP query: adding multiple fields in an array?

How do I combine these two fields (category name and posts per page) into one line of code?

<?php $query = new WP_Query( array( 'category_name' => 'investor-news' ) );?>
<?php $query = new WP_Query( array( 'posts_per_page' => 5 ) );?>

Upvotes: 1

Views: 193

Answers (2)

aomanansala
aomanansala

Reputation: 90

Alternatively, you can use something like this:

query_posts(array('category_name' => 'investor-news', 'posts_per_page' => 5));

while(have_posts()) : the_post();
    //display post code here
endwhile; wp_reset_query();

Upvotes: 0

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

Based on Manual:-https://codex.wordpress.org/Class_Reference/WP_Query

You can do it like below:-

$args = array(
    'category_name' => 'investor-news',
    'posts_per_page' =>  5
);
$query = new WP_Query( $args );

Upvotes: 2

Related Questions