Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer
    
modified as OP requirement –  Gembel Intelek Jun 22 '12 at 0:43
    
This worked perfectly! Thanks! –  KingLiNoodles Jul 5 '12 at 22:42

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

share|improve this answer
    
that's great, thanks! –  Gembel Intelek Jun 21 '12 at 12:25
    
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! –  KingLiNoodles Jun 21 '12 at 13:25
    
Any idea where I'm going wrong with $customField = get_post_meta($post->ID,'zip',true); ? Thanks! –  KingLiNoodles Jul 4 '12 at 12:26
    
var_dump( get_post_custom( $post->ID ) ); to see if there is a value. –  toscho Jul 4 '12 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! –  KingLiNoodles Jul 4 '12 at 14:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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