I have two WordPress sites on separate domains. I need to expose the latest posts within a category via a json feed on one site that I have to use for the other site. I'll need to hit a "hidden" url, ex: /themes/mytheme/json_api.php?cat=news to get the json response.

I'd like this json feed to be a unique page without header.php and only includes the code necessary to output the posts within a specific category. What do I need to import to have access to WP_Query in this file?

3 Answers 3


Why not use an RSS widget?

  • Maybe I'm missing something, but this is more for private requests. I don't want a feed visible for people to subscribe to.
    – jaysonp
    Dec 3, 2010 at 23:06

Looks like you're trying to bootstrap WordPress. All you have to do is include the file wp-load.php that's in your WordPress root. This makes most WP functionality (like the WP_Query class) available to your custom script.


Obviously, you'll need to adjust the path to the include, and write your own query/output stuff.


I would suggest that you use the AJAX API. This will boot the WordPress Core for you, and provide you with most of the functions you would expect, including the $wpdb global and the WP_Query class. You can also use WordPress' permission system and nonce functions.

To make this work, instead of a separate file create a callback function with your script and hook it to the AJAX system.

function ajax_callback_wpse_4870() {
  // your script

Your Javascript then POSTs to /wp-admin/admin-ajax.php with an action parameter named your_callback_hookname.

As implied by the hook names, the first hook is for logged in users, the second is for users who are not logged in. You don't need both hooks if you only want one or the other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy