最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Returning a custom content types with meta values

programmeradmin1浏览0评论

I find myself facing a situation where I will have a bunch of custom post types and I want to select those with specific meta values. There is a slight wrinkle in that the value I want to select against is within a larger array stored to a single meta key.

I'm pretty sure I could write an SQL select statement using LIKE "%$myterm:$myvalue%" but I wondered if there was an easier way?

I find myself facing a situation where I will have a bunch of custom post types and I want to select those with specific meta values. There is a slight wrinkle in that the value I want to select against is within a larger array stored to a single meta key.

I'm pretty sure I could write an SQL select statement using LIKE "%$myterm:$myvalue%" but I wondered if there was an easier way?

Share Improve this question asked Jun 12, 2019 at 15:08 Matthew Brown aka Lord MattMatthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges 2
  • 1 Not an answer to the question, but if not storing the data in an array is possible, then do that. When storing an array it's saved in a "serialised" form, which is not designed for querying. You've have a much much easier time (and much better performance to boot) if you save the values separately. – Jacob Peattie Commented Jun 12, 2019 at 15:15
  • That's what I suspected. The data is not just foo=>bar but foo=>(bar=>baz,lar=>nar) where foo, baz, and nar could literally be anything. I'm halfway tempted to rejig it with a custom table - at least as an index of the foo vs baz. – Matthew Brown aka Lord Matt Commented Jun 12, 2019 at 15:26
Add a comment  | 

1 Answer 1

Reset to default 1

I think I am understanding your correctly...If I'm way off, let me know!

// Assuming ideas is an array and you are looking for ideas=>two
$query = new WP_Query(
    array(
        // You can pass 'any' to post_type, but you get a hold of revisions, attachments, and menu_items. But I am not sure how many Custom Post Types you have ¯\_(ツ)_/¯ 
        'post_type' => array('cpt_one', 'cpt_two', 'cpt_three', 'etc'),
        'meta_query' => array(
            array(
                'key' => 'ideas',
                'value' => array('one', 'two', 'three'),
                'compare' => 'IN'
            )
        )
    )
);

// ideas is still an array here, and the value of 'two' lives inside of that ideas array() 
$query = new WP_Query(
    array(
        'post_type' => array('cpt_one', 'cpt_two', 'cpt_three', 'etc'),
        'meta_query' => array(
            array(
                'key' => 'ideas',
                'value' => 'two',
                'compare' => 'LIKE' // Less-known and not explained well in the docs.
            )
        )
    )
);

Going back to Jacob Peattie's point, querying can be heavy at times depending on the complexity of your arrays. Again, if I am off here....let me know!

发布评论

评论列表(0)

  1. 暂无评论