skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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.

                    $post_title='Sample wordpress post';
                    $post_content='Sample wordpress post';
    
                    $my_post = array(
                    'post_title' => wp_strip_all_tags($post_title),
                    'post_content' => $post_content,
                    'post_status' => 'publish',
                    'post_category' => array(a)
                );
    
                // Create a new post
                $post_id = wp_insert_post($my_post);
    

    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.

    Login or Signup to reply.
  4. 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/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search