humanmade / Custom-Meta-Boxes

Lets you easily create metaboxes with custom fields that will blow your mind.
523 stars 124 forks source link

Also store a (non-ID) field value in Post/Taxonomy Select field #111

Closed jalberts closed 11 years ago

jalberts commented 11 years ago

Feature Request

This sounds a bit odd, but let me toss this out there and see if this even sounds like a remotely good idea. I'll explain by way of example.

I'm creating metaboxes under a CPT called Programs, meant to be specific to several research programs going on in one center. I want to be able to list the existing people working on that program, but I don't want to maintain a bunch of in-post name/contact info.

As it turns out, I already created a CPT for all the people in said center (named Directory). So, the best solution in my mind to not duplicating data, is to simply use a Post Select field in a metabox for the Programs CPT where post_type is directory. This field is organized within a group and is repeatable.

Everything gets stored in the serialized array as expected; post ID, etc. I haven't gotten to making the template files for the CPTs yet, but I assume I should be able to pull that ID out of the serialized array and query for a value associated with that post ID...say an email address to output. [Please let me know if I'm wrong in that assumption.]

The problem I run into in this setup is actually search. Say I search for a person's name while on the site. I expect to get their directory CPT entry, and do. I'd love to get the programs they're attached to as well, but because the value stored in the array is just the ID, I won't get any matches there.

Does it make sense to be able to possibly pass an argument to a group field that would allow you to store one or more selected values along with the ID in the array? In my example, if it also stored post_title, I'd at least get matches on the name. Not if i searched on any other fields in the post ID, such as email, but I could add in field names that I wanted to be searchable in the arguments.

Like I said, I'm not even sure this is a good idea. Maybe there's a simpler way of accomplishing what I'm trying to do. Maybe I'm just crazy?

jalberts commented 11 years ago

I gave this a bit more thought, and realized that it's not really a Group field suggestion. It's a Post/Taxonomy select one. Suggesting it for Group fields simply occurred to me because they already store a serialized array.

So, I'm changing the title of the post to reflect that. I guess the suggestion is essentially to get an option to store the select field's displayed value associated with the ID, and store the two part piece in a serialized array if that argument were passed to the field type.

mattheu commented 11 years ago

Hi...

I think I get what you mean.

Firstly, to store both ID and Title for the Post select field is fairly easy to do, but I don't think it really belongs in the core functionality. You could create a new field type that extends the post select field. I tried to create some documentation for how to do this on the wiki.

However one issue I see is that you are dealing with grouped data. Data for all fields within a grouped is stored in a single meta entry as serialized data which makes it very difficult to query/search for a specific value. But if your post select field were not part of a group, you could easily do a WP_Query for a property with a specific person etc.

Hope this makes sense.

I'm going to close the issue as I don't really think this should be a feature added to the core framework however I am happy to continue the discussion!

jalberts commented 11 years ago

Thanks for the response. I can see the argument against it being in the core class. I could also see a counter argument being made that it's reasonable to expect a page using one of the select fields to show up in the results of a site search when searching on the title value of that select field.

I'll poke around and see about extending the field type here; it'll be good practice anyway :>

I thought about ungrouping the data, but I realized that wasn't going to work for what I'm going after. My example above is a bit of a simplification. I'm not just storing people from one CPT (Directory) with a program in another CPT (Programs). I'm also associating them with a role (custom taxonomy of Programs) within that program. So, I've automatically got a grouped association of person + role, that could differ from one program/project to another for each person. I've got two fields that need to be repeatable together there.

I'm hoping I can unserialize the data within the page template to pull some additional pieces of the selected post into the current one. For instance, with the people selected by way of the post select field in the Program post, I'd like to be able to bring in their image from their Directory listing post that I'm selecting on. Does that sound like a reasonable goal?

Thanks again!

mattheu commented 11 years ago

I think it should be reasonable to do this. Including meta in search results is likely to be the harder part - but possible.

Let me know how you get on!

jalberts commented 11 years ago

Will do. My initial tests on the search indicate that I should be able to do what I want just by storing the title. Since the serialized array is just treated like a text string when WordPress performs a search.

For example, if I create a grouped field including a text field and a post select field together. If I enter my name in the text field and select my name from the post select (Directory CPT), I can get the post I'm working on to show up in a search from the front end. If I remove my name from the text field, it ceases to show in the results, because "Joe Alberts" isn't stored in the serialized array any longer, just my post_ID is via the post select field.

So, if I store the title with the post select in the serialized array by extending the field type, my theory is that it should show up when searching for my name, because my name is part of the "string" that is the serialized array.

jalberts commented 10 years ago

I've come back around to this feature, and I'm finding myself a bit out of my depths. I'm not super solid at OOP PHP. Just to re-iterate, all I'm looking to do is save the Title as well as the ID when using a select (post or taxonomy) field. Taking the post as an example, and if you don't mind me running this by you:

  1. I should be able to extend the existing CMB_Post_Select, correct? I should thereby inherit everything from CMB_Select and CMB_Post_Select.
  2. I think all I would do from there is modify the parse_save_value function...perhaps to build a serialized array of ID/Title pairs. Am I way off base there?
  3. I'm not entirely clear on what the get_delegate_data() function is, and so I'm not able to evaluate its importance in the process.

Sorry for my ignorance on this one. I'm still trying to get up to speed on more advanced stuff like this.

jalberts commented 10 years ago

@mattheu, I'm not sure if you're still following this, but I'm still having a difficult time finding the right angle of attack on this one. Any suggestions would be very welcome.