0

I am using WordPress API to show posts. How can I get custom or filtered endpoints in WordPress API? I want only the following items:

  • Post title
  • Post content
  • Post Date
  • Post categories
  • Post featured image
  • Author name, bio and image

Everything else in the API should be omitted or create a new API URL. Is there any plugin I can use to do so?

I am not very good in PHP.

  • you want to omit other datas ? or you want to hide them ? if it's just omitting, let the client omit them, there is nothing to do – mmm Jun 10 '17 at 17:32
  • I want all the items listed above in single json and it should be nested. WP api has link for every other stuff, for example featured image has link in the json that opens other json. I want all that in one endpoint. – Ganesh Jun 10 '17 at 17:58
  • you don't answer my question. I ask if you want A or B and you juste have rewrited your initial question. – mmm Jun 10 '17 at 20:23
  • i want to omit unnecessary data. – Ganesh Jun 11 '17 at 5:05
0

Here is an example of how to use Javascript to read posts from the WordPress API.

    //Pegasus = Theme name
$.getJSON('https://visionquestdevelopment.com/wp-json/wp/v2/posts', function(data){

  //foreach post
  for (i = 0; i < data.length; i++) {
    //console.log(data[i]);
    var output = '';
    var dateT;
    var dateArr = [];  

    //skip one of the posts with 404s on images 
    if( '1555'== data[i].id ) { continue; }

    //format date
    var dateTime = PegasusDateTime( data[i].date );
    dateArr = dateTime.toString().split(' ');
    //["Day of Week", "Month", "Day", "Year", "Time", "TimeZone"]
    var newTime = dateArr[1] + " " + dateArr[2] + ", " + dateArr[3]; 

    output += "Author " + data[i].author+"<br>";
    output += "Categories: " + data[i].categories+"<br>";
    output += data[i].content.rendered;
    output += "Date: " + newTime+"<br>";
    output += data[i].excerpt.rendered;
    output += "Attached Image ID: " + data[i].featured_media+"<br>";
    output += "Post Format: " + data[i].format+"<br>";
   //output += "GUID: " + data[i].guid+"<br>";
    output += "Post ID: " + data[i].id+"<br>";
    output += "Post URL: " + data[i].link+"<br>";
    output += "Custom Meta: " + data[i].meta+"<br>";
    output += "Slug: " + data[i].slug+"<br>";
    output += "Status: " + data[i].status+"<br>";
    output += "Tag IDs: " + data[i].tags+"<br>";

    $(".posts").append('<li class="item">' + output + '</li>');
  }


});

function PegasusDateTime(timeStr) {
    //eg:-timeStr=18-03-2013 12:53:00
    var tmstr = timeStr.toString().split(' '); //'21-01-2013 PM 3:20:24'
    var dt = tmstr[0].split('/'); //
    var str = dt[2] + "/" + dt[1] + "/" + dt[0] + " " + tmstr[1]; //+ " " + tmstr[1]//'2013/01/20 3:20:24 pm'
    //try to create the timestamp and set time variable to valid time
    var time = new Date(str);
    if (time == "Invalid Date") {
        time = new Date(timeStr);
    }

    return time;
} 

https://codepen.io/jimboobrien/pen/ZKrLzV

Just modify the JS to include only the information you want.

  • Jimbo
  • 1
    Hello and welcome to The WordPress Stack Exchange! Thank you for your answer but answers that only link to 3rd party websites are usually discouraged. An answer should contain all the relevant information and code necessary to complete the question. This is to ensure everyone can see it in case the website is down, blocked, or malicious. Once all the relevant code and information is in place is a good time to append reference links such as this. Please edit your question to post all the relevenat information if you could. – Howdy_McGee Jun 8 '17 at 22:27

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.