最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Display a number value from mysql query in WP

programmeradmin2浏览0评论

Anyone have a suggestion on how I can display the result of a SQL query in a WP post or page? The query statement is a SUM statement and I want to only display the resulting number value on the page. An example would be the number of pizza's sold etc.

Anyone have a suggestion on how I can display the result of a SQL query in a WP post or page? The query statement is a SUM statement and I want to only display the resulting number value on the page. An example would be the number of pizza's sold etc.

Share Improve this question asked Aug 9, 2020 at 20:24 BobBob 134 bronze badges 4
  • 1 I don't see any code in your question, what have you tried so far? Which parts elude you? – Tom J Nowell Commented Aug 9, 2020 at 21:53
  • Thank you there is no code sine I dont know where to start. I know all the sql code that I need and I know syntax is valid. It is a question of how I display that in a wordpress site that eludes me? – Bob Commented Aug 9, 2020 at 22:01
  • So you don't know how to execute the SQL query? Or you do know how to execute it but aren't sure how to put it in a post? There's at least 2 large questions in here, you'd be better off asking them separately, how to embed something in a post, and how to execute your query – Tom J Nowell Commented Aug 9, 2020 at 23:01
  • Good on the query and statement thank you. I dont know how to display (embed) that in wordpress. – Bob Commented Aug 9, 2020 at 23:58
Add a comment  | 

1 Answer 1

Reset to default 0

Your easiest route to this is probably shortcodes, which effectively allow you to insert the output from a bit of PHP code in a post or page.

For example, this would create the shortcode [countpeople] which you could pop in any post and it would render the value from the SQL statement using a $wpdb helper function:

function my_plugin_name_count_people() {
    global $wpdb;
    return $wpdb->get_var("SELECT COUNT(*) FROM PEOPLE;");
}

add_shortcode('countpeople', 'my_plugin_name_count_people');

Read more about shortcodes here: https://codex.wordpress/Shortcode_API

发布评论

评论列表(0)

  1. 暂无评论