1

This query was working fine, but now i'm having a trouble with it since i've switched to multisite. I've added "global $switched" and "switch_to_blog(4)", and it works fine when i remove the "tax_query" argument! but when i add it back it returns no results.

global $switched;
switch_to_blog(4);

$args = array( 
        'post_type' => 'grille', 
        'posts_per_page' => 1,
        'no_found_rows' => true,
        'meta_key'          => 'heure_debut',
        'orderby'           => 'meta_value',
        'order'             => 'DESC',
        'meta_query' => array(
                            'relation' => 'AND',
                            array(
                                'key' => 'type_du_programme',
                                'value' => 'emission',
                                'compare' => '='
                            ),
                            array(
                                'key' => 'heure_debut',
                                'value' => $current_time,
                                'compare' => '<'
                            )                                   
        ),
        'tax_query' => array(
                            array(
                                'taxonomy' => 'jours_de_diffusion',
                                'field'    => 'slug',
                                'terms' => $current_day
                            )
                        ) 
        );

    $loop = new WP_Query( $args );
  • Is jours_de_diffusion a registered taxonomy in the site with blog_id=4? – Pat J Oct 31 '17 at 15:37
  • it's defined in the functions.php and i can manage it from the backoffie of the blog having the id (4), so i guess yes. – hammed Oct 31 '17 at 15:44
3

I found out that in order to query wp posts via custom taxonomy, from a blog 1 to another blog 2, you have to register that custom taxonomy in both blogs, otherwhise it won't work. this is strange but true! anyways it works fine now.

  • Thank you for posting your findings here. This saved me a lot of time today! I found out that you need to have custom taxonomies registered on blog 1, if you want to use wp_set_object_terms with switch_to_blog(n), too. This somewhat makes sense after looking how switch_to_blog() works. It just changes the database and without including any plugin files and such from the switched blog. – Antti Koskinen Feb 13 at 17:51

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.