Create slugs programmatically

I’m updating programmatically posts in a WP 3.01 install, using custom made tools.
Sometimes I do change programmatically post_status from DRAFT to PUBLISHED using custom SELECT queries, however this seems spoiling posts permalinks.

When in a DRAFT state, posts have the following link structure

Read More
http://myblog.com/?p=73006

Could there be some “trick” to force a change in the link structure, generating the proper permalink?

Related posts

Leave a Reply

2 comments

  1. You need to programmatically set the slug as you do so. An SQL trigger could do the trick. Don’t forget to mind duplicate slugs as you write it.

    Else, instead of publishing using the database, write a php script that calls the WP API.

  2. Create a .php file in root of your WordPress directory and write:

    <?php
    require( 'wp-load.php' );
    
    $urunler = array(
        'order'          => 'ASC',
        'post_type'      => 'urun',
        'post_status'    => null,
        'numberposts'    => -1,
    );
    
    $tumurunler = get_posts($urunler);
    if ($tumurunler) {
      foreach ($tumurunler as $urun) {
        $urun->post_name = '';
        wp_update_post( $urun );   // Update the post into the database
      }
    }
    

    So this code loads all posts of post_type=='urun' and sets the $urun->post_name field to empty (this field defines the post permalink slug) then WordPress automatically fills a value for you when you call wp_update_post(). If you want to change post_type' to 'post' or post_type' to 'page' just change this line:

    'post_type'      => 'urun',