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.

This answer does exactly what I needed to do, but the only thing is I need it to select more than just one category.

So I have changed the code to work based on salary so salary gets entered into a custom field, then it automatically selects the salary banding category which works fine, but now I have a salary from and a salary to custom field so I have a value of salary to, so how can I get the code to add the post to mulitiple categories based on both values if that makes any sense?

Below is my code that works for the category selection based on the value of salaryband, so I need it to also select the category based on salaryband2 (custom field)

function add_my_taxonomy($post_ID) {
    $salary = array(
'1015' => '10 - 15 ph',
'100140' => '100K - 140K',
'101120' => '101K - 120K pa',
'121150' => '121K – 150K pa',
'151170' => '151K – 170K pa',
'171200' => '171K – 200K pa',
'2025' => '20 – 25 ph',
'2025k' => '20K – 25K pa',
'2630' => '26 – 30 ph',
'2630k' => '26K – 30K pa',
'3040' => '30 – 40 ph',
'3140k' => '31K – 40K pa',
'4150' => '41K – 50K pa',
'5160' => '51K – 60K pa',
'6170' => '61K – 70K pa',
'7180' => '71K – 80K pa',
'8190' => '81K – 90K pa',
'91100' => '91K – 100K pa'
    );
    $zip = array(
'1015' => '10, 11, 12, 13, 14, 15,',
'100140' => '101000, 102000, 103000, 104000, 105000, 106000, 107000, 108000, 109000, 110000, 111000, 112000, 113000, 114000, 115000, 116000, 117000, 118000, 119000, 120000, 121000, 122000, 123000, 124000, 125000, 126000, 127000, 128000, 129000, 130000, 131000, 132000, 133000, 134000, 135000, 136000, 137000, 138000, 139000, 140000,',
'101120' => '101000, 102000, 103000, 104000, 105000, 106000, 107000, 108000, 109000, 110000, 111000, 112000, 113000, 114000, 115000, 116000, 117000, 118000, 119000, 120000,',
'121150' => '121000, 122000, 123000, 124000, 125000, 126000, 127000, 128000, 129000, 130000, 131000, 132000, 133000, 134000, 135000, 136000, 137000, 138000, 139000, 140000, 141000, 142000, 143000, 144000, 145000, 146000, 147000, 148000, 149000, 150000,',
'151170' => '151000, 152000, 153000, 154000, 155000, 156000, 157000, 158000, 159000, 160000, 161000, 162000, 163000, 164000, 165000, 166000, 167000, 168000, 169000, 170000,',
'171200' => '171000, 172000, 173000, 174000, 175000, 176000, 177000, 178000, 179000, 180000, 181000, 182000, 183000, 184000, 185000, 186000, 187000, 188000, 189000, 190000, 191000, 192000, 193000, 194000, 195000, 196000, 197000, 198000, 199000, 200000,',
'2025' => '20, 21, 22, 23, 24, 25,',
'2630' => '26, 27, 28, 29, 30,',
'2025k' => '20000, 21000, 22000, 23000, 24000, 25000,',
'2630k' => '26000, 27000, 28000, 29000, 30000,',
'3040' => '31, 32, 33, 34, 35, 36, 37, 38, 39, 40,',
'2630k' => '26000, 27000, 28000, 29000, 30000,',
'3140k' => '31000, 32000, 33000, 34000, 35000, 36000, 37000, 38000, 39000, 40000,',
'4150' => '41000, 42000, 43000, 44000, 45000, 46000, 47000, 48000, 49000, 50000,',
'5160' => '51000, 52000, 53000, 54000, 55000, 56000, 57000, 58000, 59000, 60000,',
'6170' => '61000, 62000, 63000, 64000, 65000, 66000, 67000, 68000, 69000, 70000,',
'7180' => '71000, 72000, 73000, 74000, 75000, 76000, 77000, 78000, 79000, 80000,',
'8190' => '81000, 82000, 83000, 84000, 85000, 86000, 87000, 88000, 89000, 90000,',
'91100' => '91000, 92000, 93000, 94000, 95000, 96000, 97000, 98000, 99000, 100000,'
    );
    foreach ($zip as $code => $salaryband){
        $salaryband = explode(',', $salaryband);
        $customField = get_post_meta($post_ID,'salaryband',true);
        if (in_array($customField,$salaryband)){
            wp_set_object_terms( $post_ID, $salary[$code], 'salary' );
        }
    } 
share|improve this question
    
    
Thanks for response ive tried that and it just then overides the orginginal so when i duplicate it slects the salaryband custom field but then selects the salaryband2 custom fireld and just selects that category i want it to select both not just the one if that makes sense thanks –  chris Apr 24 '13 at 8:58
    
The question suggested by t3los seems to address the point to me, but I have answered a couple of similar questions lately. Perhaps they will help: wordpress.stackexchange.com/questions/91953/… and : wordpress.stackexchange.com/questions/96762/… –  s_ha_dum Apr 24 '13 at 14:07
    
Let me see if I can figure out what you are asking: You set a single custom meta field called salary and you want to set two fields based on that? What are the relationships? Why does your code only try to set one field? –  s_ha_dum Apr 24 '13 at 14:09
    
hi thanks so much for looking at this basically have a custom post type which is vaccancy in that there are a number of taxomony one being salary. within each post there are 2 custom fields salaryband and salaryband2. so what i need it to do is depending on the value of the custom field salary band i need it too add that into the salary category with a band so for examaple say salaryband was 25000 it would automatically be added to the salary category 25k - 30k which my above code does the problem is i want to assign each post to two categories in the salary category list –  chris Apr 24 '13 at 18:35

1 Answer 1

Hi thanks this is solved now i got help from a developer i know he fixed for me basically wp_set_object_terms() has a fourth argument called append. Setting that to true during the call should add the term without unsetting the already set terms.

so thats if anyone else has this issue thanks again to all that have commented thanks

share|improve this answer

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.