0
    if(isset($_GET['api']) ) {
            if ($_GET['api'] == 'json'){
                   $args = array(  
                       'post_type' => 'post'

                   );
                   $query = new WP_Query( $args ); // $query is the WP_Query Object
                   $posts = $query->get_posts();   // $posts contains the post objects


                   foreach( $posts as $post ) {    // Pluck the id and title attributes
                       $output[] = array( 
                        'id' => $post->ID, 
                        'title' => $post->post_title, 
                        'content' => $post ->post_content,
                        'imageurl' => wp_get_attachment_url( get_post_thumbnail_id($post->ID) )
                        );
                   }
                   header("content-type: application/json");


                   echo json_encode( $output );
            }
           exit();
        }  
    }

This is what I get as a result:

[
      {
        "id": 95,
        "title": "See you next year!",
        "content": "Lorem ipsum",
        "imageurl": "http://localhost/....(random url)dsad.jpg"
      },
      {
        "id": 19,
        "title": "Early bird tickets",
        "content": "that is good",
        "imageurl": http://localhost/....(random url)dsada.jpg"
      }
    ]

How can I modify it so that I will only access the ID, or the Title, not all?

  • You only want the ID and the title to return only? – David Lee Feb 9 '17 at 22:05
  • I'd like to have the URL to look like localhost/folder.../[id] , eg: 95, and then it will return only that specific object, not all – john-thomas Feb 9 '17 at 22:53
0

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

  • Yeah, but I want to have the basic idea of making json APIs – john-thomas Feb 10 '17 at 0:09
  • after adding the $args, its giving me a message false why is that? – john-thomas Feb 10 '17 at 0:13
  • ?api=json&id=95 this is the url – john-thomas Feb 10 '17 at 0:17
  • I figured it out, just had to delete the $query, one last question, is it a restful service? – john-thomas Feb 10 '17 at 0:39
  • I edited the answer so $args is used to set the ID and you can set different things there like Parameters, yeah we can say this is a very basic GET of a Restful service, given that its only returning a plain JSON. – David Lee Feb 10 '17 at 0:50

Your Answer

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

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