[英]best approach to use postmeta and usermeta? (Wordpress, Mysql Database)
What i'm trying to say is that i am developing a project where i need to save some user and post data(user id and post id) when user perform a specific action his data (post id and user id) will save in database (postmeta) and then verify with wp_query that the current user have his user id against post id or not我想说的是,我正在开发一个项目,当用户执行特定操作时,我需要保存一些用户和发布数据(用户 ID 和帖子 ID),他的数据(帖子 ID 和用户 ID)将保存在数据库中(postmeta) 然后用 wp_query 验证当前用户的用户 ID 是否与帖子 ID 相对应
if(Check if in database that current post id is saved against his current user id) // do action else // don't do if(检查数据库中当前的帖子ID是否保存在他当前的用户ID中)//做其他动作//不做
So what will be the best approach right now i'm saving his current post id and current user id in wp_postmeta and verify with wp_query if his current post id matches his current user id in wp_postmeta那么现在最好的方法是什么,我将他当前的帖子 ID 和当前用户 ID 保存在 wp_postmeta 中,并使用 wp_query 验证他当前的帖子 ID 是否与他在 wp_postmeta 中的当前用户 ID 匹配
Is the above approach is good?上面的方法好不好?
if not如果不
what will be the best approach?最好的方法是什么?
Hope you have understand my question really appreciate your answer希望你明白我的问题真的很感谢你的回答
Insertion Code:插入代码:
$new_post_id = $_POST['post_id'];
$watched="watched";
global $wpdb,$user_ID;
$cur_user_id= $user_ID;
$chstr =$wpdb->get_results( "select meta_value,meta_key from $wpdb->postmeta where meta_key = $new_post_id AND meta_value= $cur_user_id" );
if(count($chstr) > 0){
exit;
}else{
$wpdb->insert(
'wp_postmeta',
array(
'post_id'=> $new_post_id,
'meta_key' => $new_post_id,
'meta_value' => $cur_user_id
),
array(
'%s'
)
);
die();
return true;
}
Check if current user id and current post id exist:检查当前用户 ID 和当前帖子 ID 是否存在:
global $wpdb,$user_ID;
$dis_post_id=get_the_ID();
$cur_use_id=$user_ID;
$checkstr =$wpdb->get_results( "select meta_value,meta_key from $wpdb->postmeta where meta_key = $dis_post_id AND meta_value= $cur_use_id" );
if(count($checkstr) > 0){custom_text_to_display();}
Try using following code, it will save current user id in post metadata then you can retrieve posts from wp_query using meta query..尝试使用以下代码,它会将当前用户 ID 保存在帖子元数据中,然后您可以使用元查询从 wp_query 检索帖子..
function save_post_meta_userid( $post_id ) {
$curr_userid = get_current_user_id();
update_post_meta( $post_id, 'post_currentuserid', $curr_userid );
}
add_action( 'save_post', 'save_post_meta_userid' );
add_action( 'edit_post', 'save_post_meta_userid' );
Then using metaquery:然后使用元查询:
$curr_userid = get_current_user_id();
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'post_currentuserid',
'value' => $curr_userid,
'compare' => '=',
)
)
);
$query = new WP_Query($args);
echo $query->found_posts;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.