Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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

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

share|improve this question
    
Forgot to mention: once changed status from DRAFT to PUBLISHED, the permalink gets empty! –  Riccardo Oct 27 '10 at 16:26
    
Errata: Sorry for the mismatching, I mean the slug gets empty –  Riccardo Oct 28 '10 at 8:19

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
2  
calling wp_update_post should do it: codex.wordpress.org/Function_Reference/wp_update_post –  goldenapples Oct 27 '10 at 22:53
    
You mean I could use wp_update_post to update the post status? (BTW I have updatetd the title of the discussion, coz the problem lies into slugs) –  Riccardo Oct 28 '10 at 8:17
    
Yeah, that would be the above-mentioned "using a php script" option. It's the recommend one, too, since you'll then get pinging, cache flushing where needed, etc. –  Denis de Bernardy Oct 28 '10 at 13:19
    
Using wp_update_post is extremely slow... how is that? –  Riccardo Oct 28 '10 at 15:49
    
Maybe it depends on revisions being created.... –  Riccardo Oct 28 '10 at 16:03

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',
share|improve this answer
    
Thanks, nice tip! –  Riccardo Oct 29 '10 at 10:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.