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

mysql - Custom SQL Query for Wordpress page

programmeradmin1浏览0评论

I'm quite new to wordpress. I'm trying to build a tableview within a wordpress page. Therfore I need a custom SQL Query to my MySQL Database.

  1. Query data from my MySQL Database
  2. Get the result somehow in a HTML format in order to display it in a table view within a wordpress page.

My question is: How can I use the wpdb_class in a wordpress page when it is not an option to put php code in a page?

I'm quite new to wordpress. I'm trying to build a tableview within a wordpress page. Therfore I need a custom SQL Query to my MySQL Database.

  1. Query data from my MySQL Database
  2. Get the result somehow in a HTML format in order to display it in a table view within a wordpress page.

My question is: How can I use the wpdb_class in a wordpress page when it is not an option to put php code in a page?

Share Improve this question edited May 3, 2013 at 14:50 EAMann 32.2k9 gold badges88 silver badges147 bronze badges asked May 3, 2013 at 14:08 user32354user32354 811 gold badge1 silver badge3 bronze badges 6
  • 2 Use the wpdb class for custom queries in wordpress. You may also want to try the WP_Query class. – RRikesh Commented May 3, 2013 at 14:12
  • 3 Please edit your question to explain exactly what you're trying to accomplish. Telling us that you want to pull data from MySQL into a WordPress page is too vague for you to get any useful help. – Pat J Commented May 3, 2013 at 14:18
  • You are expected to have researched the problem and made an attempt at solving it before posting a question. Check the Codex page for shortcodes to start. This question is pretty broad and looks like several questions in one. See also: how to ask a good question – s_ha_dum Commented May 3, 2013 at 14:19
  • I've edited my question and I know that the wpdb_class is somehow the answer, but where do I need to put the php code that is required in order to use the wpdb_class. As I guess it needs to be placed somewhere outside of the wordpress page, I need to get the results somehow transferred from that place to the wordpress page. – user32354 Commented May 3, 2013 at 14:28
  • 1 You can either write a WordPress plugin, or you can insert your code into your theme's functions.php file. Allowing PHP code to be added directly in the WP editor is a security nightmare, to put it mildly. – Pat J Commented May 3, 2013 at 14:41
 |  Show 1 more comment

2 Answers 2

Reset to default 11

The wpdb object can be used to run arbitrary queries against the WordPress database. Let's say you want to list the most recent 4 posts:

$results = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE `post_type`='post' LIMIT 4" );

The $wpdb->posts variable will output the table name for posts. It's usually wp_posts, but if you're using a custom database prefix this could be different.

However, if you're trying to grab post data (or custom post data or meta information or user data or ... anything built-in to WordPress), you should really be using WP_Query.

Here's the same query above, re-written for WP_Query:

$query = new WP_Query(
    array(
        'post_type'      => 'post',
        'posts_per_page' => 4
    )
);

The advantage of using WP_Query is that it will automatically join against the post meta table and return an array of WP_Post objects that you can work with and loop through.

<?php 
global $wpdb;
$post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')");
    print_r($post_id); /
?>

Define global variable $wpdb then make custom query and pass $wpdb->get_results();

发布评论

评论列表(0)

  1. 暂无评论