gpt4 book ai didi

mysql - 使用 postmeta 和 usermeta 的最佳方法? (Wordpress、Mysql 数据库)

转载 作者:行者123 更新时间:2023-11-29 09:26:23 28 4
gpt4 key购买 nike

我想说的是,我正在开发一个项目,我需要保存一些用户和发布数据(用户ID和帖子ID),当用户执行特定操作时,他的数据(帖子ID和用户ID)将保存在数据库(postmeta)中,然后使用 wp_query 验证当前用户是否具有针对帖子 ID 的用户 ID

if(检查数据库中是否保存了当前帖子 ID 和当前用户 ID) //执行 Action 别的 //不做

那么现在最好的方法是什么,我将他当前的帖子 ID 和当前用户 ID 保存在 wp_postmeta 中,并使用 wp_query 验证他当前的帖子 ID 是否与 wp_postmeta 中的当前用户 ID 匹配

上面的方法好不好?

如果没有

最好的方法是什么?

希望你能理解我的问题非常感谢您的回答

插入代码:

 $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;

}

检查当前用户 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();}

最佳答案

尝试使用以下代码,它将在帖子元数据中保存当前用户 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' );

然后使用元查询:

$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;

关于mysql - 使用 postmeta 和 usermeta 的最佳方法? (Wordpress、Mysql 数据库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59538625/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com