Hamed mayahian
Hamed mayahian

Reputation: 2323

How to get_permalink of the wordpress page

I create WordPress page with this function:

function adv_activate_plugins(){
    $post_details = array(
  'post_title'    => 'بازاریابی انلاین',
  'post_name'       =>'marketing4321',
  'post_content'  => '[marketing]',
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_type' => 'page'
   );
   wp_insert_post( $post_details );

   }

How to get_permalink of that page?

Upvotes: 1

Views: 670

Answers (2)

sam sujith
sam sujith

Reputation: 111

Try with below code which would help your cause,

function get_permalink_by_post_name($post_name){
   global $post;
   global $wpdb;
   $id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$post_name."'");    
   return get_permalink($id);    
}

echo get_permalink_by_post_name('post-name');

Upvotes: 0

Mikel Porras Hoogland
Mikel Porras Hoogland

Reputation: 537

$id = wp_insert_post($post_details);
$permalink = get_permalink($id);

If you want to use it outside of your function:

function adv_activate_plugins(){
 $post_details = array(
  'post_title'    => 'بازاریابی انلاین',
  'post_name'       =>'marketing4321',
  'post_content'  => '[marketing]',
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_type' => 'page'
  );

  $id = wp_insert_post( $post_details );

  return $id;
 }

//the id of the new post
$new_post_id = adv_activate_plugins();

//get the permalink
$permalink = get_permalink($new_post_id);

Upvotes: 2

Related Questions