Is it possible to request articles that match more than one category in WordPress API?

These apis work perfectly.
http://domain.com/wp/api/get_tag_posts/?tag_slug=banana
http://domain.com/wp/api/get_category_posts/?category_slug=featured

Is there any way to join multiple tags or categories into a single request?
E.g.

Read More

http://domain.com/wp/api/get_category_posts/?category_slug=featured,news
or
http://domain.com/wp/api/get_category_posts/?category_slug=featured|news

I’m hoping to do what in SQL would be a “where category_name in (‘featured’,’news’)

Related posts

Leave a Reply

3 comments

  1. You are after this

    <?php $args = array(
    'posts_per_page' => 10,
    'category__not_in' => array(5,151)
        );
        query_posts($args);?>
    

    This is what you need to do in PHP to obtain multiple posts from multiple categories. Now in case you are also looking for json or xml or any format it spits out. Put a new function in functions.php and register it with

    add_action( 'wp_ajax_nopriv_getmyjson', 'myfunctionname' );
    add_action( 'wp_ajax_getmyjson', 'myfunctionname' );
    function myfunctionname()
    {
       Global $wpdb;
      ...
     }
    

    Call this in your theme or plugin and use action=getmyjson and url goes to admin_ajax with nonce set. After Global $wpdb you can use above function to bring all posts and then through them out as json object. Something like this

    $response = json_encode( array( 
        'success' => true,
        'message' => $ajaxmsg
        'posts' => $mypostarray
        ) 
    );
    
    // response output
    header( "Content-Type: application/json" );
    echo $response;
    
    die();   //This would then make sure it is not getting anything else out of wordpress and sends only json out.
    

    Once this is all done. You will have multiple posts out put in json format.

  2. Have you tried making the category_name an array? e.g.

    <?php $args = array(
        'posts_per_page' => 10,
        'category_name' => array('featured', 'news')
    );
    query_posts($args);?>
    

    Also, with the tag, I found this example:

    <?php query_posts('cat=32&tag=hs1+hs1&showposts=10'); ?>
    

    or

    <?php query_posts(array(
        'posts_per_page' => 10,
        'category_name' => array('featured', 'news'),
        'tag_slug__and'=>array('banana')
     )); ?>