Mysql自定义帖子类型

时间:2015-12-28 06:54:56

标签: php mysql wordpress

我正在为Wordpress plugin使用WpDataTables。但是,这是一般mySQL Query,在此之前它可以正常工作。

SELECT posts_apex_po.ID AS apex_po_ID,
posts_apex_po.post_title AS apex_po_post_title,
posts_apex_po_payment.ID AS apex_po_payment_ID,
posts_apex_po_payment.post_title AS apex_po_payment_post_title
FROM wp_posts AS posts_apex_po, wp_posts AS posts_apex_po_payment
WHERE 1=1 
AND posts_apex_po.post_status = 'publish'
AND posts_apex_po.post_type = 'apex_po'
AND posts_apex_po_payment.post_status = 'publish'
AND posts_apex_po_payment.post_type = 'apex_po_payment'

问题在于,当我尝试发布apex_poapex_po_payment个自定义帖子类型的元值时,它不起作用。

apex_po_payment有一个元键apex_po_meta.purchase_order,其值为apex_po ID

所以基本上Apex_PO是一个采购订单,当您使用apex_po_payment创建付款时,您选择apex_po ID将其链接起来。

我正在尝试显示链接的PO & PO Payment,但它不起作用。 有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

我在这里进行了一些搜索之后能够自己解决这个问题并理解mysql连接查询。我必须在同一个mysql表中对每个自定义帖子类型进行两个单独的查询,然后将它们连接到我想要的字段上。

为将来遇到同一问题的任何人发布答案。

SELECT * FROM ( 
    SELECT 
    posts_apex_po.ID AS apex_po_id,
    posts_apex_po.post_title AS apex_po_post_title,
    apex_po_meta_grand_total_tbl.meta_value AS apex_po_meta_grand_total,

FROM wp_posts AS posts_apex_po
  INNER JOIN (SELECT apex_po_meta_grand_total_tbl_posts.ID as id, meta_value, meta_key  FROM wp_postmeta AS apex_po_meta_grand_total_tbl_postmeta  INNER JOIN wp_posts AS apex_po_meta_grand_total_tbl_posts   ON apex_po_meta_grand_total_tbl_postmeta.post_id = apex_po_meta_grand_total_tbl_posts.ID   AND apex_po_meta_grand_total_tbl_posts.post_type = 'apex_po') AS apex_po_meta_grand_total_tbl
     ON apex_po_meta_grand_total_tbl.meta_key = 'grand_total' AND apex_po_meta_grand_total_tbl.id = posts_apex_po.ID 
WHERE 1=1 
   AND posts_apex_po.post_status = 'publish'
   AND posts_apex_po.post_type = 'apex_po' ) x

LEFT JOIN

( SELECT posts_apex_po_payment.post_date AS apex_po_payment_post_date,
       posts_apex_po_payment.post_title AS apex_po_payment_post_title,
       apex_po_payment_meta_amount_paid_tbl.meta_value AS apex_po_payment_meta_amount_paid,
       apex_po_payment_meta_purchase_order_tbl.meta_value AS apex_po_payment_meta_purchase_order
FROM wp_posts AS posts_apex_po_payment
  INNER JOIN (SELECT apex_po_payment_meta_amount_paid_tbl_posts.ID as id, meta_value, meta_key  FROM wp_postmeta AS apex_po_payment_meta_amount_paid_tbl_postmeta  INNER JOIN wp_posts AS apex_po_payment_meta_amount_paid_tbl_posts   ON apex_po_payment_meta_amount_paid_tbl_postmeta.post_id = apex_po_payment_meta_amount_paid_tbl_posts.ID   AND apex_po_payment_meta_amount_paid_tbl_posts.post_type = 'apex_po_payment') AS apex_po_payment_meta_amount_paid_tbl
     ON apex_po_payment_meta_amount_paid_tbl.meta_key = 'amount_paid' AND apex_po_payment_meta_amount_paid_tbl.id = posts_apex_po_payment.ID 
  INNER JOIN (SELECT apex_po_payment_meta_purchase_order_tbl_posts.ID as id, meta_value, meta_key  FROM wp_postmeta AS apex_po_payment_meta_purchase_order_tbl_postmeta  INNER JOIN wp_posts AS apex_po_payment_meta_purchase_order_tbl_posts   ON apex_po_payment_meta_purchase_order_tbl_postmeta.post_id = apex_po_payment_meta_purchase_order_tbl_posts.ID   AND apex_po_payment_meta_purchase_order_tbl_posts.post_type = 'apex_po_payment') AS apex_po_payment_meta_purchase_order_tbl
     ON apex_po_payment_meta_purchase_order_tbl.meta_key = 'purchase_order' AND apex_po_payment_meta_purchase_order_tbl.id = posts_apex_po_payment.ID 
WHERE 1=1 
   AND posts_apex_po_payment.post_status = 'publish'
   AND posts_apex_po_payment.post_type = 'apex_po_payment' ) y
ON  y.apex_po_payment_meta_purchase_order = x.apex_po_id

答案 1 :(得分:-1)

根据您提到的查询,您试图同时获得一个包含两种帖子类型的帖子。

<?php

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    class Admin extends CI_Controller {
    }

单个帖子不可能同时有两种不同的帖子类型,所以你应该在它之间进行OR。