douyingmou1389 2014-12-23 20:01
浏览 60

仅限当前用户的自定义字段 - Wordpress

What I'm trying to do is query and sum a custom field in Wordpress for the current logged in user.

I have a website for my writing company that lets writers log in and claim work that they want to do. For each assignment, there is a field called "Word Count" where I input the word count for the article that needs to be done. To calculate how much each writer is owed for an assignment, I use the word count field (with the meta key "assignment_word_count" ) and multiply it by a flat rate that each writer is paid per word.

So far the closest script I've put together that does what I want it do is an SQL script. Unfortunately, it sums ALL of the word count fields instead of just the ones that belong to the current user. If I try to add anything to the code to limit it to the current user, the total drops to zero.

<?php
$now = current_time('mysql');
                $sql = "SELECT ";
                $sql .= "meta_value FROM $wpdb->posts AS posts, $wpdb->postmeta AS postmeta ";
                $sql .= "WHERE posts.ID = postmeta.post_id AND postmeta.meta_key =     'assignment_word_count' ";
                $sql .= "AND posts.post_status = 'paid' ";
                $sql .= "AND posts.post_date < '$now' ";
                $sql .= "AND postmeta.meta_value != '' ";
                $results = array(); $values = array();
                $results = $wpdb->get_results($sql);
                $totalpay = 0;
                if (!empty($results)){
                    foreach ($results as $result) {
                        $totalpay += $result->meta_value;
                    }
                }
                echo  'Total Paid: $' . $totalpay * money_format('0.0040=(#10.2n', $number);
?>

There's also another script I found that is supposed to do exactly what I'm looking for, but it tells me that the implode line is an invalid argument.

    <?php
        //get current user
        global $current_user;
        get_currentuserinfo();
        // build query of ids by user
        $userPosts = get_posts(array('author' => $current_user->ID, 'post_type'=> 'assignments')); //change this

        // loop to create array of ids by user
        foreach ($userPosts as $post) {
            setup_postdata($post);
            $ids[] = get_the_ID();
        }
        $idList = implode(",", $ids); //tun this crap into a list

        $meta_key = 'assignment_word_count';//set this to your custom field meta key
        $totalpay = $wpdb->get_col($wpdb->prepare("
                                          SELECT meta_value
                                          FROM $wpdb->postmeta
                                          WHERE meta_key = %s
                                          AND post_id in (" . $idList . ")", $meta_key));
        echo 'Total pay: $ ' . array_sum( $totalpay);   ?>
  • 写回答

2条回答 默认 最新

  • dongtang5229 2014-12-26 12:30
    关注

    You should try first to fix your query with a hard coded user id, and then, in a second time, to work on how you pass the current user.

    In the query, the right field name is post_author, not author.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计