Short answer: you’re doing it wrong.
Long answer
update_post_meta
actually updates the field, so whenever your code runs, the value of _featured
is updated to 1
. No clicking necessary, you’re actually telling it to update the value right then and there.
What you should do is have your link point to a page that can handle the request when the link is clicked:
echo '<li><a href="' . add_query_arg( 'set_featured_id', $job->ID ) . '">Featured</a>';
Then, process that information:
add_action( 'init', function() {
$job_id = filter_input( INPUT_GET, 'set_featured_id', FILTER_VALIDATE_INT );
if ( null === $job_id ) {
return;
}
update_post_meta( $job_id, '_featured', 1 );
});
The above solution is really insecure, however. You should probably look at functions to “gate” whether the user can do this – such as current_user_can
– you could also look into doing this as part of a form submission, which would allow you to leverage wp_nonce_field
as well for some added security.