• Resolved Atlasfire

    (@atlasfire)


    Great plugin, a total saviour!

    Right now I am trying to display posts that contain a meta_value added by an ACF (Advanced Custom Fields) field. All works correctly (the correct posts are shown) if the ACF field is a single-select field type, however if the ACF field is a multi-select field type no posts are shown.

    Here is my current shortcode:

    [display-posts meta_key=”brand_focus” meta_value=”123″ image_size=”full” wrapper=”div” include_excerpt=”false” excerpt_length=”20″ excerpt_more=”…” posts_per_page=”9″ columns=”3″]

    From what I can tell, when the ACF field is mutli-select, the meta_value outputs become serialized arrays looking something like… a:2:{i:0;s:1:”123″;i:1;s:2:”346″}

    Is there a way to make the shortcode meta_value search these serialized arrays for the value needed, in this case 123?

    This article seems to be answering that it is possible (https://barn2.co.uk/querying-posts-by-custom-field-acf/), but I’m not sure how to implement this into your plugin?

    Thanks in advance for any hints, tips and help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Bill Erickson

    (@billerickson)

    It’s unfortunate that ACF is storing that as a serialized array. You really shouldn’t be querying data that’s in a serialized array.

    If it was me, I’d either use a different metabox tool like Carbon Fields or CMB2, OR I’d write some code hooked into save_post that looked for that serialized array, unserialized it, and stored it as a proper array using a different meta key.

    Display Posts Shortcode does not currently have parameters for advanced meta queries. We’re doing a simple meta_key and meta_value, but not a full meta_query so you don’t have access to parameters like compare.

    You can filter the query arguments and modify them as necessary. For instance, the following code will copy the last example in that article you linked to if the meta_key specified is brand_focus: https://gist.github.com/billerickson/aa5c4557609cd35c605771f0a6220d5f

    This is untested, but should provide you decent direction if it doesn’t work. You can place this in your theme’s functions.php file, a core functionality plugin, or a code snippets plugin.

    Thread Starter Atlasfire

    (@atlasfire)

    Thanks for such an in-depth answer! Hugely helpful!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display posts if meta_value included in ACF multi-select’ is closed to new replies.