Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

325
Views
How do i use custom post types in separate JS file?

I have made a custom Wordpress post type recepies and I'm trying to use them in a separate JS file but can't find an easy way to access them? (I'm also using JQuery if that makes it easier?)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can access the data of your custom post types in a javascript file using Rest API and jQuery.

For example, you want to display the ID of your recipe in a div with the class of "the_id" and the title of the recipe in a div with the class of "the_title". Moreover I'm assuming your post type has the name of "recipes" and we just want to get one recipe.

$(document).ready(function() {
    $.ajax({
        url: "http://yoururl.com/wp-json/wp/v2/recipes?per_page=1"
    }).then(function(data) {
       $('.the_id').append(data[0].id);
       $('.the_title').append(data[0].title.rendered);
    });
});

You can reduce the number of posts you are getting with the "per_page" parameter.

In data you can now access everything the Rest API gives you, in the example the ID (via data.id) and the title (via data.title.rendered).

This might interest you: https://developer.wordpress.org/rest-api/reference/

Here the fields you can access by default: https://developer.wordpress.org/rest-api/reference/posts/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!