如何使用wordpress钩子函数在发表文章的同时添加一条记录
<span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>我要对文章进行排序,按照投票数的多少排,已经投票的都会记录投票数,而没有投票的文章没有记录,默认投票为0,这时排序就有问题了,它先把有记录的文章进行排序,因为投票可能是负数,排完了负数再排列没有投票记录的文章,这个时候就会出现没有投票的文章也就是投票数为0的排在负数的后面,如图: </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><img title="如何使用wordpress钩子函数在发表文章的同时添加一条记录" alt="如何使用wordpress钩子函数在发表文章的同时添加一条记录" src="https://zhuji.jb51.net/uploads/img/202305/8d474748d27b3c9fb89496a7e80b568e.jpg" style="max-width:100%!important;height:auto!important;border: 1px solid rgb(204, 204, 204); vertical-align: middle; padding: 1px; overflow: hidden; max-width: 696px; font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px; width: 653px; height: 810px;'><p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
<img title="如何使用wordpress钩子函数在发表文章的同时添加一条记录" alt="如何使用wordpress钩子函数在发表文章的同时添加一条记录" src="https://zhuji.jb51.net/uploads/img/202305/6255201916618628d1918019bcd0928d.jpg" style="max-width:100%!important;height:auto!important;border: 1px solid rgb(204, 204, 204); vertical-align: middle; padding: 1px; overflow: hidden; max-width: 696px; width: 492px; height: 201px;"> <br><br>
投票记录表 <br><br>
生成的sql语句:SELECT wp_posts.ID FROM wp_posts LEFT JOIN wp_wti_like_post on wp_wti_like_post.post_id=wp_posts.ID WHERE 1=1 AND ( ( post_date_gmt > ’2013-11-16 12:17:03′ ) ) AND wp_posts.post_type = ‘post’ AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_status = ‘private’) ORDER BY wp_wti_like_post.value DESC,wp_posts.post_date DESC LIMIT 5, 5 <br><br>
sql语句应该怎样写呢? <br><br>
解决办法: <br>
使用wordpress的钩子函数,在发表文章的同时添加一条记录到投票表不就可以了? <br><br>
直接上代码: <br><br>
</p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode5" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
//发表文章的同时插入数据到喜欢表function new_article($post_ID){ global $wpdb; $ip = WtiGetRealIpAddress(); $query = "INSERT INTO {$wpdb->prefix}wti_like_post SET "; $query .= "post_id = '" . $post_ID . "', "; $query .= "value = '0', "; $query .= "date_time = '" . date('Y-m-d H:i:s') . "', "; $query .= "ip = '$ip'"; $success = $wpdb->query($query); if($success){ return $post_ID; }}add_action('publish_post', 'new_article'); </div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>把这段代码放到主题function.php里面。</span>
頁:
[1]