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.

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

报告相同问题?

悬赏问题

  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含