I`m trying to add new post to wordpress site. I use direct insert query to mysql database to wp_posts column. The problem is that the guid column is empty, for other posts it has url in it. How i can generate url to the guid column for new records using query?
I tried using uuid() but it creates id, not the url.
4
Answers
That’s not a good practice. WordPress has functions to create/update posts that take care of that for you, no need to reinvent the wheel.
Read about wp_insert_post() function. You just get the contents of your post ready and pass it to the function.
This way, wordpress will sanitize and validate the data in order to avoid errors or conflicting duplicates.
Also, there’s usually routines (from plugins and themes) set to run every time a post is created. If you create directly in the DB, those routines don’t get triggered.
That column, despite its name, does not contain guids in the Paul Leach / RFC4122 sense. It contains unique identifiers that look like URLs. Generally they relate to some sort of permalink. But you don’t necessarily need to populate that column.
If you’ll create posts / pages / media /etc with a program rather than interactively, you really must, at a minimum, understand WordPress’s wp_insert_post() function. You can read its source code in the php part of any WordPress install, or here.
Most people needing to insert posts do so with a plugin.
It is not good practice to insert data through insert query into wordpress post table in order to create new posts.
Use below function to insert data into wordpress it will automatically create guid and rest of required columns.
If you have data in any other format then store it in custom table, fetch data from your custom table and create each row as WordPress post using above function.
This may be needed if you have custom columns in your mysql table.
You need
wp_unique_post_slug
function.https://developer.wordpress.org/reference/functions/wp_unique_post_slug/