What is proper way to store info such as user marking a post as favorite? In post meta or user meta?

I have custom post types such as course, lesson, and lesson_topicand I have ajax call to mark a lesson_topic as favori

I have custom post types such as course, lesson, and lesson_topic and I have ajax call to mark a lesson_topic as favorite for a user. I am currently doing this in the ajax handler:

function topic_mark_favorite () {
    if ( !wp_verify_nonce( $_REQUEST['nonce'], "topic_mark_favorite_nonce")) {
        file_put_contents(ABSPATH.'wp-content/debug.log', 'Something went wrong' . PHP_EOL, FILE_APPEND);
        exit("Something went wrong...");
    }

    $id = $_REQUEST["post_id"];
    $user_id = get_current_user_id();
    $result=0;
    if ($user_id) {
        $result = update_post_meta($id, 'user_favorite', $user_id);
    }
    if ($result) {
        $result_type= 'success';
        $result = ['type' => $result_type];
    } else {
        $result = ['type' => 'error'];
    }
...
}

Wondering if this is sensible or if I should be doing this in fundamentally different way.

I thought about using user meta and having meta field such as favorites but then I would have to store JSON data there in order to extract what I need given a context.

For instance, depending on view I might want to get user favorites for a given lesson. Or for the entire course. Because I have existing post meta relationships I can always get lesson_topics for current lesson or for all lessons in a course. So that is why I thought about continuing using post meta but can't help but think maybe I am approaching this wrong.

Above said, I am thinking about just doing away with post meta and using custom tables...

Curious to hear thoughts!

Brian

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745041066a4607809.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信