View wordpress posts with SQL

Asked

Viewed 119 times

0

I’m making a static site, where all the files are in PHP, however this site has a blog that should be manageable by Wordpress, then you ask me and pro what not to do everything by Wordpress? because it is rsrsr order are orders right.

Then I had the idea to create a folder called "news" where I will install Wordpress, so it will be domain/news.

Only that I need to display the last two news on the home page, ie outside of Wordpress, I know I will have to create a file to connect to the database and do the include na index.php and write a sentence in SQL, but I am very lay in this, could give me a light?

1 answer

2


I know I’ll have to create a file to connect to the database data and include in index.php and write a sentence in SQL

You don’t need any of this. As of version 4.7 you can use the REST Wordpress API to pull the data. The only requirement is that you are using Pretty-permalinks (anything other than seusite.com?p=342 for example).

$posts = file_get_contents( 'http://seuwordpress.com/wp-json/wp/v2/posts' );

$posts is now a JSON object containing the last posts on the site. Equivalent to what would be the home page.

  • Pretty-permalinks is the same as friendly url right? then you mean that with this I can use the native sql functions of Wordpress on any page outside Wordpress? I will already test how it works

  • this, url friendly. You won’t be able to use the native functions of Wordpress. What you can do is requests for the endpoints of the API, which will return you parts of the content - like the one I showed you. The link has all the endpoints available and examples of how to make queries.

  • In fact it is simply as if you are consuming an external webservice, you make requests and receive answers.

  • I got :D I open his return with that Postman program that organizes all the json code and it doesn’t show the featured image of the post, would you tell me?

  • It will not show the image, it will only bring the ID and you need to make another request to fetch this image by the ID. Open another question with that question if you need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.