0

I'm trying to do execute a query with multiple parameters and can't figure out how to properly format the query. There isn't any documentation on combining queries in codex.wordpress.

I need to have the following in the query:

  • category id - to only show posts from a certain cateogry (or categories, array)
  • number of posts per page
  • order by date DESC
  • exclude a post using a post id

Here is my attempt:

   <?php
   $query = new WP_Query('cat=4', array('posts_per_page' => '4'), array('orderby' => 'date','order' => 'DESC'));
if ($query->have_posts()) : while ( $query->have_posts()): $query->the_post(); ?>
        <li><a href="<?php the_permalink() ?>">
        </li>
  <?php endwhile; else: ?>
    <p>Sorry, nothing</p>
  <?php endif; wp_reset_postdata(); ?>

Any help is appreciated! Thanks!

1 Answer 1

3

You can use & to separate parameters like in URL GET parameters:

new WP_Query('cat=4&posts_per_page=4&orderby=date&order=DESC')

Or you can supply an array parameter:

new WP_Query(array(
    'cat' => 4,
    'posts_per_page' => 4,
    'orderby' => 'date',
    'order' => 'DESC'
))
Sign up to request clarification or add additional context in comments.

2 Comments

Sergiu, that works great. Thank you. How would I pass in multiple categories? Would I pass in an array into that hash or define the whole 'cat' => x multiple times?
Pass it in as an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.