我正在尝试获取每一篇文章,在元键中有一个特定的URL_cornerstone_data
. 从WordPress纪录片中,我可以构建这个查询,这可能让我接近我想要实现的目标:
$meta_query_args = array(
array(
\'key\' => \'_cornerstone_data\',
\'value\' => $attachment_url,
\'compare\' => \'LIKE\',
),
);
$meta_query = new WP_Meta_Query( $meta_query_args );
$mysql =$meta_query->get_sql(
\'post\',
$wpdb->posts,
\'ID\'
);
结果是一个如下所示的数组:
\'join\' => string \'INNER JOIN fx30k_postmeta ON ( fx30k_posts.ID = fx30k_postmeta.post_id )\'
\'where\' => string \'AND ( ( fx30k_postmeta.meta_key = \'_cornerstone_data\' AND fx30k_postmeta.meta_value LIKE \'{c6fc…
AND ( ( fx30k_postmeta.meta_key = \'_cornerstone_data\' AND fx30k_postmeta.meta_value LIKE \'{c6fc239abf1a24a4f555ce84bb7edf17dd16d1cd29f38c738529c38aafc546ff}https://usd1.wireless-empire.de/wp-content/uploads/usd-ISO-27001-Foundation-Seminar-2018.pdf{c6fc239abf1a24a4f555ce84bb7edf17dd16d1cd29f38c738529c38aafc546ff}\' )
)
然而,我不知道如何处理这个结果,我也没有找到任何纪录片或教程来解释这一点。我怎样才能进一步利用这个,得到我正在寻找的帖子?
<小时>
NEW APPROACH
这是我解决这个问题的新方法,这里包含了一些建议。然而,我得到了很多错误的结果:
$meta_query_args = array(
\'post_type\' => array( \'post\', \'page\' ),
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'_cornerstone_data\',
\'value\' => $attachment_url,
\'compare\' => \'LIKE\',
)
),
);
$meta_query = new WP_Query( $meta_query_args );
$posts = $meta_query->posts;
当我将其放在普通SQL中时,应该发生以下情况:
SELECT *
FROM `wp_postmeta`
WHERE `meta_key` = \'_cornerstone_data\'
AND `meta_value` LIKE \'%https://example.com/wp-content/uploads/this.pdf%\'
这个查询实际上返回了正确的结果。我怎么把这个放在
WP_QUERY
?
<小时>
Printing out the SQL query that is being generated with my query, it looks like this:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) JOIN wp_icl_translations t
ON wp_posts.ID = t.element_id
AND t.element_type = CONCAT(\'post_\', wp_posts.post_type) WHERE 1=1 AND (
( wp_postmeta.meta_key = \'_cornerstone_data\' AND wp_postmeta.meta_value LIKE \'{645cebb1ce3cff4f917092440a092979319a39817e68ccd213b321a8cef9f388}https://example.com/wp-content/uploads/this.pdf{645cebb1ce3cff4f917092440a092979319a39817e68ccd213b321a8cef9f388}\' )
) AND wp_posts.post_type = \'post\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'future\' OR wp_posts.post_status = \'draft\' OR wp_posts.post_status = \'pending\' OR wp_posts.post_status = \'private\') AND ( ( ( t.language_code = \'de\' OR 0 ) AND wp_posts.post_type IN (\'post\',\'page\',\'x-portfolio\' ) ) OR wp_posts.post_type NOT IN (\'post\',\'page\',\'x-portfolio\' ) ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10
我不知道这个查询中的这个很长的字符串应该是什么。这是应该有的地方
%
. 但即使我接受此查询,也要替换
%
然后在我的数据库上试用(使用phpMyAdmin),它不会返回正确的结果。