0

Here's the function I have in my theme's functions.php:

function better_editions( $query ) {

    // Categories act as 'Editions' on my site

    $get_edition = get_query_var( 'category_name' );
    if( $get_edition !== '' ) {
        $edition = get_term_by( 'slug', $get_edition, 'category' );
        if( $edition && !is_wp_error( $edition ) ) {
            $edition = $get_edition;
        }
    }

    if ( $query->is_category() && isset($edition) && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post' ) );
        $query->set( 'category_name', $edition.', intl' );
    }
}
add_action( 'pre_get_posts', 'better_editions' );

I use the function to display posts that belong to 'International (intl)' category under all categories (in all category archives, category posts listings, etc).

As I learnt, in order to define multiple categories with category_name in $query->set();, this is the only way, unless I am wrong:

$query->set( 'category_name', 'category1, category2' );

Which is what I am doing, as shown in the function far above.

The problem is, with the function in place, I get this error:

Notice: Array to string conversion in /var/www/wp.dev/public/wp-admin/edit.php on line 225

The error is only visible with debug turned on, and only on category-specific "All Posts" page. For example...

http://wp.dev/wp-admin/edit.php?category_name=intl

...the above link lists all posts under "International (intl)" category in wp-admin.

What am I doing wrong in the function? How do I fix this?

NOTE: I don't want to use !is_admin() because I want the function to apply to wp-admin as well.

1

I am unable to duplicate the problem, but the Notice references this line:

add_screen_option( 'per_page', array( 'label' => $title, 'default' => 20, 'option' => 'edit_' . $post_type . '_per_page' ) );

And that makes me think that you should be passing a post type in your GET string.

http://wp.dev/wp-admin/edit.php?post_type=post&category_name=intl

Just a guess as I can't duplicate the issue. If that doesn't move you forward I will delete the answer :)

  • Whoa! That tip did it! It's that I am setting the post type on this line: $query->set( 'post_type', array( 'post' ) );. Removing it from the function resolved the problem, while retaining all functionality. Thank you very much! If possible, please edit your answer to make note of this. – its_me Nov 23 '13 at 18:30
  • On my phone, I'll update later. Glad it helped. – s_ha_dum Nov 23 '13 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.