Getting and updating a wordpress post from permalink

November 8, 2011

We’ve been working on a plugin and we needed to get a wordpress page from a given permalink. This is actually done pretty easily since there is a function wordpress uses deep in their system.

$post_id = url_to_postid($permalink);

This will give you the post_id which you will able to use to retrieve the post object.

$post = get_post($post_id);

Now that we have the object its pretty simple to manipulate it.

$post->post_content = $new_page_content;
wp_update_post( $post );

So all together:

$post_id = url_to_postid($permalink);
$post = get_post($post_id);
$post->post_content = $new_page_content;
wp_update_post( $post );

Author

Stay in Touch!

Stay in Touch!
Solutions Architecture

browse through our blog articles

Blog Archive