0

Let's say we have three custom fields:

  • Pirates
  • Robots
  • Ninjas

Maybe we're saving information about different people who are related to a post in each of those professions.

What I'm trying to figure out is how to save that all into one custom field, let's call it Professions, when the page is Updated or Published (ie, when the page is saved in some way).

Bonus would be to add the content to that field to.

Why? This makes it a lot easier to include this stuff in default WP search results.

Any ideas?

  • This is somewhat confusing. Why roles should be anywhere in the content, and what does anything here has to do with search? Meta data is not part of the default search – Mark Kaplun Jun 4 '16 at 3:04
  • This is hard to answer unless you show where the information is being saved to already... but basically you would hook to edit_post (after the info has saved) and there get the information you need and save it to a single post meta as an array. – majick Jun 4 '16 at 5:20
  • @MarkKaplun "Why roles should be anywhere in the content?" This has nothing to do with roles, those are just examples of what the custom fields might be about. They could just as easily be "State", "City" and "ZIP" or "Cat", "Dog", and "Turtle". And no, meta data is not part of the default search, but it's easy enough to include one custom field in search results. Anyway, if it's too complicated to think of it that way, the main question is just how to save all custom fields into one "master" custom field. – Nathan Jun 4 '16 at 14:50
  • @majick The data in each is stored in a custom field. So it's easily retrieved with get_post_meta($post->ID, 'pirates', true); for example. – Nathan Jun 4 '16 at 14:51
1

This is assuming each of the fields is a single value and you want to create a list for display...

add_action('edit_post','custom_combine_fields');

function custom_combine_fields($post_id) {

    $pirates = get_post_meta($post_id,'pirates',true);
    $robots = get_post_meta($post_id,'robots',true);
    $ninjas = get_post_meta($post_id,'ninjas',true);

    $professions = array();
    if ($pirates) {$professions[] = $pirates;}
    if ($robots) {$professions[] = $robots;}
    if ($ninjas) {$professions[] = $ninjas;}

    if (count($professions) > 0) {
        $professions = implode(', ',$professions);
        update_post_meta($post_id,'professions',$professions);
    }
}

I don't see much great advantage to doing this on save rather than in a display template but if you really want to that should do it.

  • Nice, I'll give this a try and report back. :) – Nathan Jun 7 '16 at 20:24
  • Took me awhile to have a chance to test this but works like a charm. The advantage to saving them all into one is that it's much easier to do a search on one big field vs. searching individual custom fields, so once they're combined, a search for "hook" can return "Captain Hook" from 'pirates' and 'grappling hook' from 'ninjas', if that makes sense. – Nathan Sep 1 '16 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.