根据自定义字段值将自定义帖子自动分配给自定义分类

时间:2012-06-21 作者:KingLiNoodles

我们有一个自定义的post类型,名为listings, 以及每个listing 具有自定义字段zipcode (这是一个5位数)。然后我们有一个自定义分类法area, 可以是“纽约”、“洛杉矶”等。

如果zipcode 如果是“10001”、“10002”或“10003”,则应将其分配给“纽约”,如果是“90001”或“90101”,则应将其分配给“洛杉矶”,依此类推。

我们如何自动分配listingarea 基于zipcode 价值

谢谢你的帮助!

2 个回复
最合适的回答,由SO网友:Gembel Intelek 整理而成

我相信这不是最好的答案,不过你可以这样尝试

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\' );
        }
    }
}
我从这里得到了Zipcode列表

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

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

SO网友:fuxia

修改this answer 使用元字段而不是标题。其他任何东西都应该是一样的。

结束