【问题标题】:best approach to use postmeta and usermeta? (Wordpress, Mysql Database)使用 postmeta 和 usermeta 的最佳方法? (Wordpress、Mysql 数据库)
【发布时间】:2019-12-31 01:34:15
【问题描述】:

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

if(检查数据库中当前的帖子 ID 是否保存在他当前的用户 ID 中) // 做动作 别的 // 不要这样做

那么,现在最好的方法是什么,我将他当前的帖子 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();}

【问题讨论】:

  • 能否提供代码示例?
  • @Aliqua 刚刚更新了我的问题

标签: mysql database wordpress


【解决方案1】:

尝试使用以下代码,它会将当前用户 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;

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-12
  • 2012-06-07
  • 2011-10-09
  • 2023-03-18
  • 2012-10-01
  • 2014-12-24
  • 1970-01-01
相关资源
最近更新 更多