0

We have a custom post type called listings, and each listing has a custom field zipcode (which is a 5 digit number). We then have a custom taxonomy area, which can be "New York", "Los Angeles" etc.

If the zipcode is "10001", "10002" or "10003" then it should be assigned to "New York", and if it's "90001" or "90101" then it should be assigned to "Los Angeles" and so on.

How can we automatically assign each listing to an area based on the zipcode value?

Thanks for any help!

2 Answers 2

1

I'm sure this not best answer, however you may try like this

add_action('save_post', 'add_my_taxonomy');
function add_my_taxonomy($post_ID) {
    $area = array(
    'ny' => 'New York',
    'la' => 'Los Angeles'
    );
    $zip = array(
    'ny' => '10001, 10002, 10003, 10004, 10005, 10006, 10007, 10008, 10009, 10010, 10011, 10012, 10013, 10014, 10015, 10016, 10017, 10018, 10019, 10020, 10021, 10022, 10023, 10024, 10025, 10026, 10027, 10028, 10029, 10030, 10031, 10032, 10033, 10034, 10035, 10036, 10037, 10038, 10039, 10040, 10041, 10043, 10044, 10045, 10046, 10047, 10048, 10055, 10060, 10069, 10072, 10079, 10080, 10081, 10082, 10087, 10090, 10094, 10095, 10096, 10098, 10099, 10101, 10102, 10103, 10104, 10105, 10106, 10107, 10108, 10109, 10110, 10111, 10112, 10113, 10114, 10115, 10116, 10117, 10118, 10119, 10120, 10121, 10122, 10123, 10124, 10125, 10126, 10128, 10129, 10130, 10131, 10132, 10133, 10138, 10149, 10150, 10151, 10152, 10153, 10154, 10155, 10156, 10157, 10158, 10159, 10160, 10161, 10162, 10163, 10164, 10165, 10166, 10167, 10168, 10169, 10170, 10171, 10172, 10173, 10174, 10175, 10176, 10177, 10178, 10179, 10184, 10185, 10196, 10197, 10199, 10203, 10211, 10212, 10213, 10242, 10249, 10256, 10257, 10258, 10259, 10260, 10261, 10265, 10268, 10269, 10270, 10271, 10272, 10273, 10274, 10275, 10276, 10277, 10278, 10279, 10280, 10281, 10282, 10285, 10286, 10292',
    'la' => '90001, 90002, 90003, 90004, 90005, 90006, 90007, 90008, 90009, 90010, 90011, 90012, 90013, 90014, 90015, 90016, 90017, 90018, 90019, 90020, 90021, 90022, 90023, 90024, 90025, 90026, 90027, 90028, 90029, 90030, 90031, 90032, 90033, 90034, 90035, 90036, 90037, 90038, 90039, 90040, 90041, 90042, 90043, 90044, 90045, 90046, 90047, 90048, 90049, 90050, 90051, 90052, 90053, 90054, 90055, 90056, 90057, 90058, 90059, 90060, 90061, 90062, 90063, 90064, 90065, 90066, 90067, 90068, 90070, 90071, 90072, 90073, 90074, 90075, 90076, 90077, 90078, 90079, 90080, 90081, 90082, 90083, 90084, 90086, 90087, 90088, 90089, 90091, 90093, 90094, 90095, 90096, 90097, 90099, 90101, 90102, 90103, 90174, 90185'
    );
    foreach ($zip as $code => $zipcodes){
        $zipcode = explode(',', $zipcodes);
        $customField = get_post_meta($post_ID,'zipcode',true);
        if (in_array($customField,$zipcode)){
            wp_set_object_terms( $post_ID, $area[$code], 'area' );
        }
    }
}

I got those zipcodes list from here

http://www.city-data.com/zipmaps/New-York-New-York.html

http://www.city-data.com/zipmaps/Los-Angeles-California.html

1
  • modified as OP requirement
    – Dazilio
    Commented Jun 22, 2012 at 0:43
3

Modify this answer to use the meta field instead of the title. Anything else should be the same.

6
  • Wow fantastic! It's now working from the post_title but I can't get it working from the zip. I've added $customField = get_post_meta($post->ID,'zip',true); to the beginning, and replaced all post_title instances with $customField. When I update the post it says Cannot access empty property. Any idea how to use the custom field here? Thanks! Commented Jun 21, 2012 at 13:25
  • Any idea where I'm going wrong with $customField = get_post_meta($post->ID,'zip',true); ? Thanks! Commented Jul 4, 2012 at 12:26
  • var_dump( get_post_custom( $post->ID ) ); to see if there is a value.
    – fuxia
    Commented Jul 4, 2012 at 12:28
  • toscho, you've been so helpful in this. I really appreciate it, thank you! Unfortunately I'm not sure where exactly to use var_dump here.. For reference, here's the plugin as it stands now (which isn't working yet, but when $zip was post_title it worked perfectly from the title - I just need to replace the title with my custom field): pastebin.com/Fhsi9gfx . Any thoughts would be very helpful! Commented Jul 4, 2012 at 14:04
  • I've used var_dump in the template, and it's shown a value for the custom field: ["zip"]=> array(1) { [0]=> string(5) "10001" . Commented Jul 5, 2012 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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