Wordpress wp_insert_post () redirect?

2019-08-27 00:09发布

问题:

Now i create a wordpress front-end post, after wp_insert_post(); i want to redirect into the full page of that post data. Here i use the this way, so it was redirect the blog index page, but i need this page redirect this post full page.

      $post_insert = array(
        'post_title'    => $title,
        'post_content'  => $content,
        'tags_input'    => $post_tags,
        'post_status'   => 'publish',
      );
      $post_id = wp_insert_post( $post_insert );
      wp_redirect( site_url()."?post=$post_id" );
      exit();

回答1:

I think the issue is with $post_id being inside the double quotes. Try this instead:

wp_redirect( site_url()."?post=".$post_id);


回答2:

It is better to use this:

wp_redirect(get_permalink($post_id));

The get_permalink method is the Wordpress built-in method to get a permanent link. See this url for the documentation for this method: http://codex.wordpress.org/Function_Reference/get_permalink



标签: wordpress