json - How do I use URL to fetch all posts of a particular custom post type?

admin2025-06-06  1

I'm trying to be able to fetch all the posts of a particular post type on my website. The post type should be called 'opportunities'. I'm new to wordpress so a bit out of my depth. The posts in question actually have their on button on the admin sidebar so they could be something entirely different from a 'custom_post_type'. Any help I could get on this would be much appreciated.

*edit: These are the notes I got from a developer I'm trying to help:

"To access the database with opportunities from the UK office we need them to make these available through an API. This can be done by their developer by changing a few lines in their code. It’s explained at: / after making the changes I need to know the URL where I can retrieve the opportunities through JSON."

I'm not a developer, I'm more or less just the guy that got asked to help with website in our office.

I'm trying to be able to fetch all the posts of a particular post type on my website. The post type should be called 'opportunities'. I'm new to wordpress so a bit out of my depth. The posts in question actually have their on button on the admin sidebar so they could be something entirely different from a 'custom_post_type'. Any help I could get on this would be much appreciated.

*edit: These are the notes I got from a developer I'm trying to help:

"To access the database with opportunities from the UK office we need them to make these available through an API. This can be done by their developer by changing a few lines in their code. It’s explained at: https://developer.wordpress/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/ after making the changes I need to know the URL where I can retrieve the opportunities through JSON."

I'm not a developer, I'm more or less just the guy that got asked to help with website in our office.

Share Improve this question edited Oct 29, 2018 at 13:56 Luke asked Oct 26, 2018 at 13:43 LukeLuke 11 bronze badge 10
  • Your question is not clear. Can you please elaborate more. So, We'll help you. – maheshwaghmare Commented Oct 26, 2018 at 13:47
  • These are the notes I got from a developer I'm trying to help: "To access the database with opportunities from the UK office we need them to make these available through an API. This can be done by their developer by changing a few lines in their code. It’s explained at: developer.wordpress/rest-api/extending-the-rest-api/… after making the changes I need to know the URL where I can retrieve the opportunities through JSON." – Luke Commented Oct 26, 2018 at 13:59
  • Do you have set the 'show_in_rest' => true and 'rest_controller_class' => 'WP_REST_Posts_Controller' parameters while registering your post type opportunities? If yes the you can access them with rest api endpoint like: example/wp-json/wp/v2/opportunities – maheshwaghmare Commented Oct 26, 2018 at 14:06
  • Like default post rest api endpoint - yoursite/wp-json/wp/v2/posts. Change http://yoursite with your domain and try yourself. It'll works! If not then share your website URL for reference. – maheshwaghmare Commented Oct 26, 2018 at 14:07
  • Thanks for your quick answers. Where would I find that in the code to make sure it's set to true? I have put it in the theme functions but not sure if that was right place. – Luke Commented Oct 26, 2018 at 14:17
 |  Show 5 more comments

1 Answer 1

Reset to default 0

I think what you mean is do an onclick event.

So, you can do this with JavaScript. Create your link as per usual <a id=“myCustomLink” href=“#” onclick=“MyFunction();return false;”>huh! I’m a link!</a>

And now some script

<script>
function MyFunction() {

//Make it do whatever here! For example:

document.getElementById("demo").innerHTML = "Hello World";
}
</script>

Rerun false; will prevent the browser directing to the link... unless you want it to? If so then just remove this and add a link in-place of the # after the href.

However! A much cleaner way of doing this would be to do it as JQuery and attach the ID as an onclick handler. Like so:

$(‘#myLink’).click(function(){ MyFunction(); return false; });

Hope this helps!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749224093a317400.html

最新回复(0)