vivek
vivek

Reputation: 326

Can't get parmalink from Wordpress database

i want to fetch all post and insert into custom table but i can't get url (Parmalink) of post but it's always null or get error.

$posts = get_posts(array(  "showposts" => 50));
global $wpdb;
foreach($posts as $post) 
{
      $wpdb->insert('wp_employee', array('pottitle'=>$post->post_title, 'postid'=>$post->ID, , 'postid'=>$post->permalink), array('%s', '%s', '%s'));
}

Upvotes: 1

Views: 69

Answers (3)

Savan Paun
Savan Paun

Reputation: 1733

Try this and let me know if any issue ;)

function getp($s)
    {
        $result = post_permalink( $s );
        return $result;
    }

Call the function

$posts = get_posts(array(  "showposts" => 50));
global $wpdb;
foreach($posts as $post) 
{
      $wpdb->insert('wp_employee', array('pottitle'=>$post->post_title, 'postid'=>$post->ID,  'postid'=>$post->getp($post->ID)), array('%s', '%s', '%s'));
}

Also you can call direct with using post_permalink

'lastname'=>post_permalink($post->ID))

Upvotes: 2

WordpressCoder
WordpressCoder

Reputation: 303

You can use the following line inside the foreach loop

$permalink = get_permalink($post->ID);

and use $permalink wherever you want in foreach loop

Upvotes: 0

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

get_posts() does not return value like permalink

try to check http://codex.wordpress.org/Template_Tags/get_posts

so you need to make manually permalink with postid or use get_permalink() or the_permalink()

Upvotes: 0

Related Questions