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.

Hello I have a question which is maybe more PHP question than WordPress, but could be interesting in a WordPress context in case someone wants to add properties to an WP object.

I have created a custom taxonomy and I'm using an option to add order to each terms.

To get that order in my theme, i use those two functions : the first one retrieve the terms, and then they get ordered :

    // SORT BY ORDER 
    function cmp( $a, $b ) { 
        if(  $a->order ==  $b->order ){ return 0 ; } 
        return ($a->order < $b->order ) ? -1 : 1;
    } 

    // FUNCTION TO INJECT ORDER ITEM IN TERM OBJECT
    function inject_order( $term_objects ) {
        if ( $term_objects ) {
            foreach ( $term_objects as $term_object ) {
                $term_id = $term_object->term_id;
                $term_order  = get_option( "taxonomy_term_$term_id" )['tax_order_nb'];
                $term_object->order = $term_order;
                array_replace( $term_objects, $term_object );
            }
            return $term_objects;
        } else {
            return false;
        }
    }

    // FUNCTION TO GET CHILDREN TERMS AND ORDER THEM
    function get_children_and_order_terms( $term_id = '0' ) {
        $term_children = get_terms( 'category_effectif', array( 'parent' => $term_id, 'hide_empty' => 0 ) );
        if ( $term_children ) {
            $term_children = inject_order( $term_children ); 
            usort ( $term_children, 'cmp' );
            return $term_children;
        }
    }

It's working perfectly, but I have a PHP_Warning array_replace(): Argument #2 is not an array

I would like to correct that error and make it better.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

$term_object->order tells you $term_object is an object. array_replace() expects an array, not an object.

Not sure why it works, maybe PHP is casting the object to an array.

array_replace( $term_objects, (array) $term_object );

… could fix the warning.

share|improve this answer
    
Works like a charm ! I checked before posting code to transform an object to an array but it was somehow complicated. Your solution works perfectly : order is still injected, without warning now. Thanks. –  Simon Apr 22 '13 at 17:55
    
There are a number of places in WordPress where a function or method claims it takes either an array or an object, but actually does both at different times, depending on what calls it. Could be this is the result of one of those. –  Chris Krycho Apr 22 '13 at 18:21
2  
@ChrisKrycho True, but array_replace() is a built-in PHP function. –  toscho Apr 22 '13 at 18:24
    
Of course you're correct. I had misread the original question; I thought he was dealing with something other than he actually was. That'll teach me to read more carefully. –  Chris Krycho Apr 22 '13 at 20:39

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.