dongzangchui2072 2015-08-30 00:26
浏览 56
已采纳

使用具有多个术语的MYSQL在wordpress中重新索引自定义表

I am currently working on re-indexing a custom table in my Wordpress database. I am collecting the data values sucessfully, however I am struggling to collect all $wpdb->terms.

Certain posts have more than one term associated with them, for example I have a custom post type (stores) and within this a custom taxonomy (products-sold) and within this custom taxonomy I have a list of 4 terms (Term A, Term B, Term C, Term D).

I have managed to fill the new database with the information needed except Colomn "post_terms", the first term gets added but I need to add the other terms associated (if they exist) so that the cell displays each slug seperated by a comma (slug-A, slug-B).

            $sql = "";
            $sql .= "INSERT INTO $this->tablename (`post_id`, `post_type`, `post_terms`, `lat`, `lng`) ";
            $sql .= "SELECT * FROM (";
                $sql .= "
                    SELECT
                        `$wpdb->posts`.`ID` AS `post_id`,
                        `$wpdb->posts`.`post_type`,
                        `$wpdb->terms`.`slug` AS `post_terms`,
                        `lat_field`.`meta_value` AS `lat`,
                        `lng_field`.`meta_value` AS `lng`
                    FROM
                        `$wpdb->posts`
                        INNER JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
                        INNER JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
                        INNER JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
                        LEFT JOIN `$wpdb->postmeta` AS `lat_field` ON (`$wpdb->posts`.`ID` = `lat_field`.`post_id` AND `lat_field`.`meta_key` IN ('".implode('\', \'',$this->keys['lat'])."'))
                        LEFT JOIN `$wpdb->postmeta` AS `lng_field` ON (`$wpdb->posts`.`ID` = `lng_field`.`post_id` AND `lng_field`.`meta_key` IN ('".implode('\', \'',$this->keys['lng'])."'))
                    WHERE `$wpdb->posts`.`post_status` = 'publish' AND (`lat_field`.`meta_value` IS NOT NULL OR `lng_field`.`meta_value` IS NOT NULL) AND " . $wpdb->term_taxonomy . ".taxonomy = 'products-sold'
                ";

            $sql .= ") AS `t`";
            $sql .= " ON DUPLICATE KEY UPDATE `lat` = VALUES(`lat`), `lng` = VALUES(`lng`)";
            $wpdb->query($sql);

This is the working code I have above, any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • dongwei3151 2015-08-30 00:34
    关注

    No time for a long, exasperation answer ....

    You'll want to:

    GROUP_CONCAT(slug, ',')

    and add a GROUP BY post_id

    This will show all slugs concatenated for the row and separated by a comma. These slugs will be matched and grouped on post_id. Where you should have a distinct post_id entity per result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能