[wp-hackers] Why in the heck won't this work?

jacobsantos at branson.com jacobsantos at branson.com
Thu Aug 23 13:55:40 GMT 2007


Makes sense.

You are looking for a key value that doesn't exist, because the array 
key is created not by category id (wouldn't be unique), but numerically. 
You will have to either do an array_filter function which will iterate 
through each element and check the objects for the id or do a manual 
looping. No other way around it, unless you either want to use SQL, or 
ask for arrays instead of objects. The latter is possible, but the 
specifics eludes me at this moment and I lack time to find the answer 
for you.

It is highly likely that the function you are looking for has a third 
parameter for returning array.

Jacob Santos

Viper007Bond wrote:
> Yes, as is term_id, but having to loop through each object in the array is a
> pain, especially when the $categories array should have the term_id /
> term_taxonomy_id as the key as it is in in_category().
>
> On 8/23/07, DD32 <wordpress at dd32.id.au> wrote:
>   
>> Perhaps this will help?
>>
>> term_taxonomy_id is the Category ID
>>
>>
>> <?php
>> $post_ID = 28;
>>
>> $categories = wp_get_object_terms($post_ID, 'category');
>>
>> var_dump($categories);
>> ?>
>>
>> array
>>     0 =>
>>       object(stdClass)[165]
>>         public 'term_id' => string '9' (length=1)
>>         public 'name' => string 'Test Cat' (length=14)
>>         public 'slug' => string 'test-cat' (length=14)
>>         public 'term_group' => string '0' (length=1)
>>         public 'term_taxonomy_id' => string '9' (length=1)
>>         public 'taxonomy' => string 'category' (length=8)
>>         public 'description' => string '' (length=0)
>>         public 'parent' => string '0' (length=1)
>>         public 'count' => string '1' (length=1)
>>     1 =>
>>       object(stdClass)[166]
>>         public 'term_id' => string '14' (length=2)
>>         public 'name' => string 'Wordpress Plugins' (length=17)
>>         public 'slug' => string 'wordpress-plugins' (length=17)
>>         public 'term_group' => string '0' (length=1)
>>         public 'term_taxonomy_id' => string '14' (length=2)
>>         public 'taxonomy' => string 'category' (length=8)
>>         public 'description' => string '' (length=0)
>>         public 'parent' => string '0' (length=1)
>>         public 'count' => string '1' (length=1)
>>
>> On Thu, 23 Aug 2007 21:06:10 +1000, Viper007Bond <viper at viper007bond.com>
>> wrote:
>>
>>     
>>> Perhaps coding at 4am isn't the best idea, I know, but I can't figure
>>>       
>> out
>>     
>>> for the life of me why this won't work.
>>>
>>> I've made of copy of in_category() and modified it slightly to accept
>>>       
>> the
>>     
>>> post ID rather than getting it from the global $post (due to use in a
>>> plugin
>>> -- I need to find out if a certain post is in a certain category).
>>>
>>> My code: http://wordpress.pastebin.com/f5c7e4b6a
>>>
>>> The original's $categories array's keys are the values of the category
>>> ID.
>>>
>>> ex:
>>>
>>> Array
>>> (
>>>     [6] => stdClass Object
>>>         (
>>>             [term_id] => 6
>>>
>>>
>>> However, the one in my function does it sequentially:
>>>
>>> Array
>>> (
>>>     [0] => stdClass Object
>>>         (
>>>             [term_id] => 6
>>>
>>>
>>> Then 1, then 2, etc.
>>>
>>> Due to this, the array_key_exists() test is failing.
>>>
>>> Any ideas why? I don't see what is making the difference.
>>>
>>>       
>> --
>> Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
>>     
>
>
>
>   



More information about the wp-hackers mailing list