I want to update posts meta value in the wp_query to a different value on click. What I am basically doing is making a notification bubble (like stackoverflow).
wp_query
with the number of posts that have meta value 'unread' - WORKING- Show the count bubble on top op logged in user's display name - WORKING
- On clicking the notification, hide the bubble and change post meta value with ajax only for the posts in the
wp_query
from 'unread' to 'read' so that the count is only visible next when there's a new post with meta value 'unread' - HOW? - Show a dropdown of all latest posts with meta value 'read' - maybe another query?
Here's my code for count bubble.
add_shortcode('notify', 'notify_member');
function notify_member() {
global $current_user;
get_currentuserinfo();
$userid = $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 );
$html = '';
$html .= '<a href="#" class="count">'.$questions->found_posts.'</a>'; //total count
return $html;
}
I have looked at ajax like and vote tutorials, they tackle one post at a time. I will need the below to work for all returned posts from the query and change at the same time.
if( $questions->have_posts() ) {
while ( $questions->have_posts() ) { $questions->the_post();
update_post_meta( $post->ID, '_read_answer', 'read' );
}
}
I am out of ideas, any direction is welcome. Thanks. P.S. I am not a coder, so please do explain a little.