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.

I get an undefined Offset message with this Query. Can anyone Help?

$terms = get_the_terms( $post->ID , 'filters', 'string');
        $term_ids = wp_list_pluck($terms,'term_id');
        $second_query = new WP_Query( array(
              'post_type' => 'portfolio',
              'tax_query' => array(
                            array(
                                'taxonomy' => 'filters',
                                'field' => 'id',
                                'terms' => $term_ids,
                                'operator'=> 'IN' //Or 'AND' or 'NOT IN'
                             )),
              'posts_per_page' => 4,
              'ignore_sticky_posts' => 1,
              'orderby' => 'date',  // 'rand' for random order
              'post__not_in'=>array($post->ID)
           ) );
share|improve this question
    
If one of the answers solved your problem mark it as answer – click the checkmark on the left side. If not, ask for clarification. –  toscho Oct 6 '12 at 22:08
    
He did ask for clarification and the answer poster never responded. –  Tom Thorogood Mar 13 '13 at 12:32
add comment

2 Answers

When you use wp_list_pluck on $terms what you get is an array where the key is the same as the value. If you dump the contents of $term_ids you'll see something like:

array(
    [42] => 42,
    [56] => 56
}

The code in query.php tries to reference the zero index of the term_ids array, but there is no index 0, hence the warning.

share|improve this answer
1  
Okay, so what to do? :) I'm really not that advanced on this... :/ –  Phil Sep 27 '12 at 18:30
add comment

As Milo pointed out your array is broken (the error told you that already). To get an array WP can work with use array_values():

$term_ids = array_values( wp_list_pluck( $terms,'term_id' ) );
share|improve this answer
    
Thanks a lot!! :-) –  Phil Oct 10 '12 at 8:24
add comment

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.