Forums › Forums › Search & Filter Pro › Is it possible to combine multiple meta keys into one post meta field dropdown?
Tagged: combining meta keys, post meta field
- This topic has 6 replies, 2 voices, and was last updated 6 years, 4 months ago by Anonymous.
-
Anonymous(Private) July 11, 2018 at 4:41 pm #182352
Hello,
I currently have a S&F form that returns 2 post types, Posts and Resources.
On my Resources CPT, I have an ACF field for “Resource Type.” However, on my Resources Page I want to combine the Resources CPT and Blog posts together, blog posts being a type of resource even though it is not an option on the resources cpt, those being created in the standard “posts” post type.
On my Resources Page I have an S&F form with 3 fields, one being “Resource Type” which is a Post Meta field using the “Resource Type” ACF field for the meta key. But since blog post is not an option in the Resource Type ACF field, I”m not sure how I can add it to the Resource Type Meta Field dropdown on my Resources Page.
I was thinking I could add some hidden meta key to the “posts” post type so that it always has a value of “blog” but still are not sure how I could combine that meta key with the Resources Type meta key I’m using in the Resources Type Post Meta field…hope I didn’t lose you there lol.
Is there a hook I could use to combine these? Or any advice would be greatly appreciated.
Thanks!
Anonymous(Private) July 11, 2018 at 5:08 pm #182361I was thinking that originally, but might get confusing or annoy my client. If they were in the Resources CPT, they wouldn’t ever select blog post, as those are create in the Posts post type section. And if they were creating blog posts in the Posts post type section, they would never select anything other than blog post from the resources type acf field.
That was why I was hoping there was a way to combine meta keys in a single Post Meta field dropdown.
Anonymous(Private) July 11, 2018 at 5:21 pm #182363I probably should specify that the Resources Type ACF field does NOT contain an option for Blog Posts now. My last comment was under the assumption that I would add it to the Resource Type ACF field in order to add the field group to both Resources and Posts Post type. AS of now, the Resources Type ACF field only appears on the Resources CPT
Anonymous(Private) July 11, 2018 at 9:33 pm #182369So I ended up figuring out a hacky work around. If anyone else runs into this situation what I did was add Blog Posts to my Resource Type ACF Field (changed from checkbox to select field) as the first option and included it on both Posts and Resources post types.
Then I used the following action:
add_action('admin_head', 'hide_checkbox_option'); function hide_checkbox_option() { // Hide Resource Type Field Group if( get_post_type() == 'post' ) { ?> <style> #acf-group_5b465bd8ba093 { visibility: hidden; } </style> <?php } // Hide first <option>, which is Blog Post if( get_post_type() == 'resources' ) { ?> <style> #acf-field_5b465be2c6c7f option:first-child { display: none; } </style> <?php } }
This basically hides the field group altogether on blog posts, and since blog post is the first option, it will automatically be selected for each newly created blog post even though the user will never see it.
Then it’s hiding the first option in the Resource Type select field on the Resources CPT. So that way the client doesn’t get confused seeing Blog post as an option.
Like I said, I realize this is super hacky, and I would be open to other suggestions. But I’ve tested it out and it works, so will have to do for now.
Trevor(Private) July 12, 2018 at 6:58 am #182380Alternatively, on the WP ‘save_post’ (https://codex.wordpress.org/Plugin_API/Action_Reference/save_post), you could automatically add an action to add the post_meta, so that the user does not have to select it?
-
AuthorPosts