在元关键字中获取每个有价值的帖子

时间:2018-10-07 作者:TheKidsWantDjent

我正在尝试获取每一篇文章,在元键中有一个特定的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),它不会返回正确的结果。

1 个回复
SO网友:Piotr Kucułyma

Try this:

$meta_query_args = array(
    \'meta_query\'=>array(
        array(
            \'key\' => \'_cornerstone_data\',
            \'value\' => $attachment_url,
            \'compare\' => \'LIKE\',
        ),
    )
);

$meta_query = new WP_Query( $meta_query_args );

$posts = $meta_query->posts;
结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post