• Any helper function to get posts that have a specific value on a repeatable CMB2 field?

    Because the data is stored as an array neither get_posts or a SQL query will do.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    None I know of offhand, probably due to the potential complexity and the performance for such an item, where you’re trying to search post meta across many posts. On top of that, hard to predict what the value would be, since it could technically be any variety of text.

    What type of functionality are you implementing where you need this type of thing, perhaps we can help determine a different route to try? At least for your case, what meta value would you be searching for?

    Regarding the original part, curious if something could be brained up where one provides the meta key and the desired meta value. You could then grab all the posts that have that meta key existing and is not null, and then from that pool, pluck out the ones where the value exists. Just throwing some ideas out there.

    On top of that, hard to predict what the value would be, since it could technically be any variety of text.

    Search by meta value is a pretty standard WordPress thing to do.

    What type of functionality are you implementing where you need this type of thing, perhaps we can help determine a different route to try?

    It’s a generic question, but I can give you an example.
    While migrating an old website to WordPress, I’m setting the old page URL on a custom field, so i can use it for redirects when the new website is up and someone tries to visit a old URL. I need to search by meta value to identify the WordPress post/page/cpt/… that is now replacing that old URL. On the specific website I’m now working, unfortunately, each page could be accessed by several different URLs so I might need to insert more than one value on the same custom field.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I realize it is, but the detail is in that the repeatable field data is serialized.

    From a widget in my localhost install: a:2:{i:1;a:0:{}s:12:"_multiwidget";i:1;}
    vs
    '_multiwidget'

    It does add complexity because you can’t just set the meta query to look for _multiwidget. It also depends on what exactly is being stored. Sentences of content? or something predictable like in your case with your latest reply: urls.

    Anyways.

    I know meta keys can have different meta values and still be valid, may or may not help you here, without need for potential repeater fields.

    Otherwise, without diving too far into it myself mentally, perhaps store a setup of “old_url” and “new_url” both as post meta, with the old one being what you’re redirecting from, and the new one being the current permalink. That way you could easily and consistently fetch based on the meta key.

    Just some thoughts, use if you want ??

    Thanks for your reply.

    I’ll have to setup 2 or 3 non-repeatable fields and search on them all.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Search posts by meta value if field is repeatable’ is closed to new replies.