php - 自定义 WordPress SQL 查询以根据帖子 ID 输出元数据 - php、数据库(自定义帖子类型和字段)

标签 php mysql wordpress pivot-table associative-array

我正在从另一个 Wordpress 数据库运行数据库查询,因此使用标准的 sql 查询而不是各种 wordpress 函数。

本质上,我想显示与 postID 关联的所有元值/数据。我已经设置了一个客户帖子类型“事件”,它有自己的自定义字段(开始日期等)。

我几乎拥有我需要的一切,我只需要编写适当的 php 循环来输出每个帖子 ID 的数据(以显示帖子的元数据)。

Table Structure Image

注意:meta_values 可以为 null,将来可能会添加更多 meta_keys。

$newdb = //already setup new db connection (don't worry about this)
$query =
                "
                    SELECT * 
                    FROM wp_posts, wp_postmeta
                    WHERE wp_posts.ID = wp_postmeta.post_ID
                    AND wp_postmeta.meta_key NOT LIKE '\_%'   
                    AND wp_posts.post_type='event' 
                    AND wp_posts.post_status = 'publish';
                " ;  


$events = $newdb->get_results($query, OBJECT);
//get_results() is a wordpress function, nearly equiv to my_sqli
//OBJECT - result will be output as an object.
//ARRAY_A - result will be output as an associative array.


foreach ( $events as $event ) {
echo $event->post_title;
echo $event->meta_value;    
}

当前结果:

标题_1,2016 年 2 月 11 日

标题_1,05:00

标题_1,2016 年 1 月 12 日

Title_2,2016 年 2 月 5 日


**期望的结果:**(针对每个帖子)

(结构上):

post_ID:1,post_title:Title_1,start_date:11/02/2016,start_time:05:00,end_date:12/01/2016

$event->post_title;
$event->meta_value”;  //meta_key = start_date
$event->meta_value”;  //meta_key = end_time
$event->meta_value”; //meta_key = end_date

(视觉上)

标题_1, 2016 年 11 月 2 日, 05:00, 2016 年 12 月 1 日,

标题_2, 2016 年 5 月 2 日, 07:00, 2016 年 7 月 2 日,

最佳答案

我最终创建了自己的枢轴 php 循环,该循环检查每个结果数组(来自 SQL 查询)并根据帖子的 ID 将其分组到另一个关联数组中。一切都运行得非常好,我可以将每个帖子的元值放在要显示的适当 HTML 标记中。

这是我选择自定义帖子类型(“事件”)和自定义字段(存储在元键值对中)的完整工作代码:

//credentials can be found in your wp-config.php file
$newdb = new wpdb('DB_USER', 'DB_PASSWORD', 'DB_NAME', 'DB_HOST');

//sql query
$querystr =
        "
        SELECT wp_posts.ID, wp_posts.post_title, wp_postmeta.meta_key, wp_postmeta.meta_value
        FROM wp_posts, wp_postmeta
        WHERE wp_posts.ID = wp_postmeta.post_ID
        AND wp_postmeta.meta_key NOT LIKE '\_%'
        AND wp_posts.post_type='event'
        AND wp_posts.post_status = 'publish';
        " ;

//wordpress function that returns the query as an array of associative arrays
$results = $newdb->get_results($querystr, ARRAY_A);

//create an empty array
$events = array();
foreach ($results as $row) {
    //if row's ID doesn't exist in the array, add a new array
    if (!isset($events[$row['ID']])) {
        $events[$row['ID']] = array();
    }
    //add all the values to the array with matching IDs
    $events[$row['ID']] = array_merge($events[$row['ID']], array('post_title'=>$row['post_title']), array($row['meta_key']=>$row['meta_value']));
}

//extract data from each event
foreach ($events as $event) {
    var_dump($event); //do something
    echo $event['start_date']; //can also echo specific metavalues etc.
}

关于php - 自定义 WordPress SQL 查询以根据帖子 ID 输出元数据 - php、数据库(自定义帖子类型和字段),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35957435/

相关文章:

php - MySql - 加入另一个多行表,将查询插入另一个查询?

mysql - 插入忽略选择 LAST_INSERT_ID()

mysql - 如何使用 Slick 在 VARCHAR 列中使用 UUID?

php - 您如何扩展Themosis helpers.php函数?

php - DIV的高度自动调整

php - glob() 有否定吗?

php - 什么是数据库中的父表和子表?

wordpress - 自定义帖子类型 Slug 冲突

wordpress - Woocommerce:仅显示来自同一子类别的相关产品

javascript - 按列排序 div 并防止垂直自由空间