WP_Insert_Post and GUID Issue [Wordpress]

2019-07-07 08:16发布

I have a posting form at my theme. I used wp_insert_post there. My code looks like this

$post = array(
      'ID' => '',
      'post_author' => $post_author,
      'post_category' => $post_category,
      'post_content' => $post_content,
      'post_title' => $post_title,
      'post_status' => 'publish',
    );  

    $post_id = wp_insert_post($post);
    $fullpost = get_post($post_id);

    wp_redirect($fullpost->guid);

Everything works fine. But when it's inserted to the database, at the GUID field, the entry format is like this permalinks_structure/id. So if my permalinks_structure is like /category/id , it become like http://www.example.com/uncategorized/1. So the problem is, if i post through wordpress admin panel, GUID of the post is http://www.example.com?p=1 . So my post database GUID become mess, because if i want to change permalinks_structure , the post which becomes from outside form will follow the structure.

What I want to do is, I want to get the GUID like that I posted through admin panel which is http://www.example.com?p=1

How can i do it ? Please point me out. Thank you.

标签: php wordpress
2条回答
我只想做你的唯一
2楼-- · 2019-07-07 08:51

I think the short answer is to not worry about the GUID of a post in WordPress. Here's a thread discussing the (in)validity of a post's GUID: http://www.mail-archive.com/wp-testers@lists.automattic.com/msg12434.html

If you ever need a post's URL, you should use the get_permalink() function. This will always respect your current permalink structure, and is the safest way to get post's URL

查看更多
时光不老,我们不散
3楼-- · 2019-07-07 09:11

Return #

(int|WP_Error) The post ID on success. The value 0 or WP_Error on failure.

So .. check for integer and do this to get the URL $url = get_permalink($post_id);

This bypasses the direct call to the guid

查看更多
登录 后发表回答