try this:
if (isset($_GET['api'])) {
if ($_GET['api'] == 'json' && isset($_GET['id'])) {
$args = array(
'p' => $_GET['id'],
'post_type' => 'any'// ID of a page, post, or custom type
);
$query = new WP_Query($args); // $query is the WP_Query Object
$post = $query->post; // $post contains the post object
header("content-type: application/json");
echo json_encode($post);
}
exit();
}
get post will retrieve a single post, it returns an array so it can go directly to json_encode, you will have to add to the URL “id=XX” where X is a number.
If you have the latest version of wordpress i recommend you to use the built-in api WP REST Api
Related Posts:
- WordPress REST API not returning all posts
- exclude particular category in api
- Fetch All Posts (Including Those Using a Custom Post Type) With WordPress API
- WP Rest API v2 filter and display latest post with specific tag
- Disable REST API for a user ROLE
- ping_status in JSON REST API
- How to create a post using REST API with sending data as a JSON body?
- How can I get the media inside a post?
- How to disable WordPress blog folder
- Call Web Services on post first publish
- Creating a WordPress Post via REST API – HTML or Markdown?
- Optimal WP Web App Backend for mobile app development
- Pull in posts using post meta for REST API
- REST API Working for GET but not for POST?
- Loading posts outside wordpress
- Need All the posts with id, title and date [closed]
- How to use “Add link” pop up for a WordPress widget
- How to add category to: ‘wp-admin/post-new.php’?
- WP REST API only returning partial list of users
- Adding additional data to WP_Post object
- Passing a hardcoded page/post ID into `get_post`
- Use an attachment in multiple posts
- WP REST API, query total posts in a category
- JSON not valid after json_encode posts
- Get post from custom REST endpoint in Gutenberg
- WordPress Rest API – Get all posts based on post_meta on custom endpoint
- Implementing the wp json-rest api
- WordPress REST API “rest_authentication_errors” doesn’t work external queries?
- Query Post Preview Updates from the WP API?
- Increase the page size of the WordPress REST API
- WordPress blog posts api – get posts by author
- Options to get my custom post type metadata via the WordPress API
- WP rest api returns 404 only when author param is used
- Every possible way to get data (posts) from WordPress
- Keep user’s privileges on accessing contents in JSON response
- Create post using JSON api plugin
- How can I cache WordPress Rest API Response
- Exclude posts w/ specific post_meta on Rest API endpoint
- Why aren’t tags and categories added in post request to WP Rest API
- Add ajax load more function using WP API
- Transfer WordPress post to RoR site: is the REST Api the only and/or best solution?
- JSON – Permission Error?
- Exclude post_meta from Rest API Endpoint
- WP Remote Request not returning all of content via request
- Get more than one author’s posts with REST API
- WordPress Rest API Create Post
- body_class REST field in WP-API
- Change post-slug using wordpress API? Change permalink of a post using php / jquery?
- Getting featured image with PHP and not javascript from wordpress api _embed [closed]
- search blog posts by author first name and or last name
- How to get post with associated categories and tags names instead of ids with rest api?
- What date to use as a post date? date_gmt or modified_date_gmt
- Is there a way I can fetch the WordPress Developer Code References with an API?
- Custom wp-query in wordpress rest api
- How to clone all WordPress Rest API end points
- Get timestamp of latest post in C#
- Filter out posts based on a certain post_meta on the WordPress REST API
- how to use nimble-API and Display data?
- Delete Post but retain image of post [WP REST API]
- Rest API Paginate until all posts are imported
- Format array of objects to json for wp-json
- Add a field in my rest api
- How do I assign an attachment to a post using only the REST API?
- how to create JSON array [] for REST response?
- WordPress rest API get Posts parent by post ID
- Updating post status via rest api
- Weird problem occurs while using WordPress REST API to create new post
- How to receive JSON payload from a digital device
- Is update_post_meta used when save_post action hook is invoked?
- WordPress API “code”:”rest_no_route” with Custom Route
- WordPress output data to another website and pull data
- Loop through WordPress API response errors
- How to set the post permalink using the WordPress REST API?
- CRUD from WordPress to Business Central 365 through OData REST API
- Get origin or context of a how a post got created and/or updated
- How to prevent post update using transition_post_status
- Autotrader API Integration
- REST API get featured image source for custom post type
- how to fetch posts from Api in chunks or parts?
- Rest WP_Error always return null
- I would like to retrive JSON value and display it in wordpress page or widget
- Why does “if statement” has to “die()”, otherwise wont work?
- Best way to make a JSON API from WordPress?
- Problem with wp_insert_post()
- Best Way to detect unique posts in wp rest api
- Surrogate ID for posts, is there an alternative field in the posts table?
- Adding a time stamp on post view
- WP Rest API feature image breaking react component
- Changes done in admin panel are taking time to reflect in the REST api responses
- simply loop through posts
- WordPress Queue like Tumblr?
- CF7 upload file failing [closed]
- How Do I Get My Post Tags to Alphabetize?
- How to give classname to post if post has no content?
- Trying to exclude custom posts based on date, while sorting by custom field
- Taxonomy checkboxes not showing current status on edit page
- Add a url parameter to the current post and read it in a shortcode
- Query posts from newest category
- Display only posts with comments
- how to show all type of author posts in author page (SOLVED)