Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of adding a record while publishing an article by using the wordpress hook function

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use the wordpress hook function to add a record while publishing an article. I believe most people don't know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.

How do I use the wordpress hook function to add a record while publishing an article?

I want to sort the articles. According to the number of votes, those who have already voted will record the number of votes, while the articles that have not voted will not be recorded, and the default vote is 0. At this time, there is a problem with sorting. It sorts the recorded articles first, because the voting may be negative, and then arrange the articles without voting records after the negative number. At this time, there will be articles that do not vote, that is, those with a vote of 0 come after a negative number, as shown in the figure:

Voting record table

The generated sql statement:

SELECT wp_posts.ID FROM wp_posts LEFT JOIN wp_wti_like_post on wp_wti_like_post.post_id=wp_posts.ID WHERE 1mm 1 AND ((post_date_gmt > '2013-11-16 12 post_date_gmt 17D 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

How should the sql statement be written?

Solution:

Using the hook function of wordpress, can't you just add a record to the voting table while publishing the article?

Go directly to the code:

The code is as follows:

/ / insert data into favorite table function new_article ($post_ID) {global $wpdb; $ip = WtiGetRealIpAddress (); $query = "INSERT INTO {$wpdb- > prefix} wti_like_post SET"; $query. = "post_id ='" while publishing the article. $post_ID. "',"; $query. = "value = '0mm,"; $query. = "date_time ='". Date ('Y-m-d Hrigi'). "',"; $query. = "ip ='$ip'"; $success = $wpdb- > query ($query); if ($success) {return $post_ID;}} add_action ('publish_post',' new_article')

Put this code in the theme function.php.

The above is all the contents of the method of adding a record while publishing an article using the wordpress hook function. Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report