Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Function to make photo the post's featured image WordPress


I need a function that produces a link that when clicked will make a photo displayed in the image.php template file the featured image of the post it is associated with. This is similar in functionality to selecting an album cover in Facebook.

The function only needs to produce the full uri that will set the featured image.

One method of determining the photo/image/attachment's parent is using this function that I got from twentyeleven: esc_url( get_permalink( $post->post_parent ) );

Edit: This is for use on the front end.

Answers (3)


Jurre Hanema answers:

Try adding this to your functions.php:

add_filter('query_vars', 'wpq_query_vars');
add_action('wp', 'wpq_wp');

function wpq_query_vars($qv)
$qv[] = 'set_featured';

return $qv;

function wpq_wp()
if(is_attachment() && get_query_var('set_featured'))
global $post;

update_post_meta($post->post_parent, '_thumbnail_id', get_the_ID());

function wpq_set_featured_link($post_id = 0)
$post_id = get_the_ID();

return add_query_arg(array('set_featured' => 'true'), get_permalink($post_id));

In your image.php you can add the link as follows:

<a href="<?php echo wpq_set_featured_link(); ?>">Click me!</a>

Matt Taylor comments:

That works! On a related note, do you know if there is a conditional tag to check if the post is the featured image? You get my vote regardless...

Thank you!


Abdessamad Idrissi answers:

You need it in the admin? or front-end?
This is also known as post thumbnails and your theme should support it.

To enable post thumbnails, the current theme must include
add_theme_support( 'post-thumbnails' );
in its functions.php

Matt Taylor comments:

Post thumbnails are enabled and in use on the site. I edited the question to note that this was for use on the front end.


Luis Cordova answers:

hi here is the answer to your reply