Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

Why not use an RSS widget?

share|improve this answer
    
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 '10 at 23:06
add comment

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.

include('wp-load.php');

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

share|improve this answer
add comment

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
}
add_action('wp_ajax_your_callback_hookname','ajax_callback_wpse_4870');
add_action('wp_ajax_nopriv_your_callback_hookname','ajax_callback_wpse_4870');

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.