After click on <a>
tage send an ajax request. I will write only php code do jQuery
part yourself.
<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
action: 'my_action'
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post(ajaxurl, data, function(response) {
//Do watever after you get response
});
});
</script>
In Your function.php
add_action( 'wp_ajax_my_action', 'my_action' );
add_action( 'wp_ajax_nopriv_my_action', 'my_action' );
function my_action(){
global $current_user;
get_currentuserinfo();
$userid = $current_user->ID; //You can also user get_current_user_id() to get current user id
$args = array(
'posts_per_page' => '-1',
'post_type' => 'answer',
'post_status' => 'publish',
'author__not_in' => $userid, //exclude user posts
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'question_owner_id'
'value' => $userid, //only show posts for the user
'compare' => '='
),
array(
'key' => '_read_answer'
'value' => 'unread', //only show unread posts
'compare' => '='
),
)
);
$questions = new WP_Query( $args ); //Get all the post whose meta you want to update
foreach($questions->posts as $question):
update_post_meta( $question->ID, '_read_answer', 'read' ); //Update all the posts meta.
endforeach;
echo true; //Send the response. You can aslo send response whatever you want.
die();
}
When ever some one click on a
tag it will send an ajax request to the server. The server run the previous code to get posts whose meta
you want to update. After getting the post update their meta
value.