I need to show popular & recent posts in another PHP web site under the same domain.


  1. www.example.com -> main website (php, mysql)
  2. www.example.com/blog -> WordPress blog

Need to show popular, recent posts of blog in the main website.

Please note that blog and main website use two separate database.

I decided to use the WP REST JSON API plugin for that. Now I have different question.

I use following code to retrieve last week popular post. What I actually need is to get this same data via API call.

$args = array(
    'date_query' => array( array( 'after' => '1 week ago' ) ),  
    'posts_per_page' => $recent_posts_count,
    'ignore_sticky_posts' => 1,
    'meta_key' => 'post_views_count',
    'orderby' => 'meta_value_num',
    'order' => 'DESC'
$popularposts = new WP_Query( $args );

How can I do that?

  • This has been asked plenty of times. Please use search.
    – kraftner
    Commented Apr 28, 2015 at 10:42
  • 1. If you have a new question please open a new question. In this case it might be okay as it isn't totally new. Anyway the title is misleading now. 2. You've seen the documentation: wp-api.org
    – kraftner
    Commented Apr 28, 2015 at 12:57
  • I have updated the title as well, however I will go through the doc, thanks Commented Apr 28, 2015 at 13:00

1 Answer 1


I will give you a small answer to your update, doing this with the WP API. The API have the possibilities to use the WP_Query like also in core, but about the get parameters in the url.

A URL to pull content from Post Status would look like this:


To pull content with WP_Query parameters you’re used to, you could do it like this:


You can build your custom query with all parameters also in the url. You can see how the method for grabbing that data feels familiar to using WP_Query for a standard WordPress loop. If you don’t specify a parameter, the defaults to WP_Query will be used.

The result is json, that you can parse and use for you external site.

See also the site of the API for more parameters and documentation.

Update for date_query

The API can't create a result for a query like query_date. See the documentation for all possible parameters.

But the new version will release in view days, weeks. And see this issue for discussion about a solution for this date query. Alternative use a custom filter via hook, like:

// Allow datequery in /posts filter
add_filter( "json_query_vars", function( $query_args ) {
    return array_merge( $query_args, 
        array( "date_query" => array( array( "after" => "1 week ago" ) ) )
} );

Update for meta_query

The API can't also this feature of the default WP-Query. But you can use a hook to enhance the API to this requirement. Also here a small example.

add_filter('json_query_var-meta_query', 'add_meta_query', 10, 1);

function add_meta_query( $data ){

    $args = array();
    $args['relation'] = 'AND';

    foreach ( $data as $key => $value ) {
        if ( 'relation' === $key ) {
            $args['relation'] = $data['relation'];

        if ( substr($key, 0, 3) === 'key' ) {
            $arg_num = substr( $key, 3 );
            $args[ (int) $arg_num ][ 'key' ] = $value;

        if (  substr( $key, 0, 7 ) === 'compare' ) {
            $arg_num_comp = substr( $key, 7 );
            $args[ (int) $arg_num_comp ][ 'compare' ] = $value;

    return $args;

Now, I can call JSON restful like that to mimic the Wp_query posts filter already on the server:


The meta query update based from this answer.

  • Do you have any idea how to convert the line 'date_query' => array( array( 'after' => '1 week ago' ) ), as a query string Commented Apr 29, 2015 at 7:01
  • I would like to accept the this as an answer, however if you have any idea regarding the above comment, you can share your thoughts. Commented Apr 29, 2015 at 7:36
  • filters other than filter[posts_per_page]=2&filter[order]=ASC this two is not working, so it is always giving recent posts only Commented Apr 29, 2015 at 8:45
  • @JanithChinthana I have tested again, the filter param works fine. - As Example http://localhost/wpbeta/plugins/wp-json/posts?filter[posts_per_page]=1&filter[order]=ASC. Much more in my update below in the answer.
    – bueltge
    Commented Apr 29, 2015 at 10:21
  • thanks for the update, yes post_per_page & order is working, but what about meta_key . does it work with this or I need to extra work for that? Commented Apr 29, 2015 at 10:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.