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'm having trouble getting post by term in a custom taxonomy I have set up. The catch is this is multisite and I'm getting this data from another blog on the network where the taxonomy is registered. The code I have works fine on the blog with which the taxonomy is registered. However when I try the same code on the other blog using switch_to_blog I get bool(false). Meaning, I guess, that there is an error in the sql. However if I just do is_term I can confirm it's there. Here's a link to the code that is working on the naitive blog but not the other,

<?php get_header(); ?>

<?php global $switched; switch_to_blog(2); ?>

<div class="row-fluid mostwanted-container">

<?php
    $type = 'portal_warrants';
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $terms = is_term( 'most-wanted' , 'warrant_status' );
    $args=array(
            'post_type' => $type,
            'posts_per_page' => 6,
            'paged' => $paged,
            'tax_query' =>
                    array(
                            array(
                                    'taxonomy' => 'warrant_status',
                                    'field' => 'id',
                                    'terms' => 'most-wanted',
                            )
                    )

            );                                       

    $my_query = null;
    $my_query = new WP_Query($args);
    $i = 0;
    if( $my_query->have_posts() ) {
            while ($my_query->have_posts()) : $my_query->the_post();

            if( $i % 3 == 0) :?>
                    </div>
                    <div class="row-fluid mostwanted-container">
            <?php endif; ?>
            <?php print_r(var_dump($wp_query->request)); ?>                
            <div class="span4 wanted">
                    <h1>Wanted</h1>
                    <div class="mostwanted-img">
                            <img src="http://placehold.it/150x170">
                            <span class="arrested">Arrested</span>
                    </div>
                    <div class="mostwanted-info">
                            <h3><?php the_title(); ?></h3>
                            <ul>
                                    <li><strong>Last Known Address:</strong></li>
                                    <li><strong>Hair:</strong></li>
                                    <li><strong>Eyes:</strong></li>
                                    <li><strong>Height:</strong></li>
                                    <li><strong>Weight:</strong></li>
                            </ul>
                    </div>
            </div> 

    <?php $i++ ?>  

<?php endwhile; } wp_reset_query(); ?> 

</div> 

<?php restore_current_blog(); ?>

<?php get_footer(); ?>

*Edit (updated pastebin with switch_to_blog included)

http://pastebin.com/uBbaXnSt

Also, this doesn't output anything at all not even with print_r(var_dump($my_query->request));


Solution (or at least in part)


The problem is switch_to_blog doesn't give you access to everything in another blog on the network including taxonomies, tags, things in your functions.php, although don't quote me 100% on that. Therefore the taxonomy wasn't registered in the "switched to" blog. My solution (at least for now) register the taxonomy in that blog. After doing that, tax_query worked as expected. This is what I'm going with until I find a better solution (if one exists at all).

share|improve this question
    
Just updated including switch_to_blog which was there before but I forgot to put in the Pasebin. The output of print_r(var_dump($wp_query->request)); is nothing at all. –  jerime May 3 '13 at 20:24
    
It looks like as long as I have tax_query in the $args I don't get any output. –  jerime May 3 '13 at 20:31
    
But you do get output from the same query if you do not try to change to some other site on the network, correct? –  s_ha_dum May 3 '13 at 20:34
    
Correct. The exact same query in the blog that the taxonomy is registered in works as expected. I've even tried copying and pasting to make sure I wasn't anything. –  jerime May 3 '13 at 20:36
    
Also, with WP_DEBUG turned on I get no errors, minus is_term is depreciated. I can dump the $terms variable and get the correct ID of the term I'm trying to get posts for. –  jerime May 3 '13 at 20:40

4 Answers 4

This is a long shot and assumes all your taxonomies are setup and being queried correctly.

But since you are using wp_reset_query() anyways try using query_posts() with your $args and use while(have_posts() : the_post()) instead of a doing a new WP_Query() class. Then restore_current_blog() before your wp_reset_query() at the end.

Also your var_dump isn't working because its $wp_query not $my_query. If you want to see the var_dump of the $wp_query object you need to global $wp_query; somewhere before it first.

Better Answer..

Don't know why I didn't notice it before.. your tax_query field is set to id not slug. Change your $args to ..

$args = array(
            'post_type' => $type,
            'posts_per_page' => 6,
            'paged' => $paged,
            'tax_query' =>
                    array(
                            array(
                                    'taxonomy' => 'warrant_status',
                                    'field' => 'slug',
                                    'terms' => 'most-wanted',
                            )
                    )

            );
share|improve this answer
    
Still giving no output with tax_query in the $args. I've got both WP_Query and query_posts() working as long as tax_query isn't in the arguments. –  jerime May 3 '13 at 20:58
    
It is $my_query because that is the new WP_Query object that the OP created. Why would you ever recommend query_posts? Especially over creating a new WP_Query object? –  s_ha_dum May 3 '13 at 21:23
    
Check my edit. I found a solution, at least for the time being. –  jerime May 3 '13 at 21:33
    
In regards to the var_dump() of $my_query vs $wp_query the code posted above has var_dump($wp_query->request) not var_dump($my_query->request) that was all. I've had strange instances of switch_to_blog not working on a new WP_Query() instance but this was closer to version 3.2 so I thought maybe it might be relevant. Forget all of that though I think this is related to my edit above. –  Cole May 3 '13 at 22:39
up vote 0 down vote accepted

Solution (or at least in part)

The problem is switch_to_blog doesn't give you access to everything in another blog on the network including taxonomies, tags, things in your functions.php, although don't quote me 100% on that. Therefore the taxonomy wasn't registered in the "switched to" blog. My solution (at least for now) register the taxonomy in that blog. After doing that, tax_query worked as expected. This is what I'm going with until I find a better solution (if one exists at all).

share|improve this answer

Same problem, my solution:

$queryterms = "
    SELECT *
    FROM wp_".$site->blog_id."_terms terms, wp_".$site->blog_id."_term_taxonomy term_taxonomy, wp_".$site->blog_id."_term_relationships term_relationships 
    WHERE (terms.term_id = term_taxonomy.term_id AND term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id)
    AND term_relationships.object_id = ".$post->ID."
    ";
$terms = $wpdb->get_results($queryterms, OBJECT);
foreach ($terms as $term) {
    // Your stuff
};
share|improve this answer

I found a small hack to filter by taxonomy without create the taxonomy in the child theme.

In the core exists the function "taxonomy_exists":

function taxonomy_exists( $taxonomy ) {
        global $wp_taxonomies;

        return isset( $wp_taxonomies[$taxonomy] );
}

And is this function who hive "false" because the taxonomy don't exists in your child theme. If this function give "false" the class "WP_Tax_Query" will not return a valid SQL to filter by taxonomy.

So, like @jerime says "My solution (at least for now) register the taxonomy in that blog".

You can "create" a the taxonomy only to do this filter, like:

// Add this before the loop
// The if is to don't touch the taxonomy if is a real taxonomy
// The "delete" value is to know this is not a real taxonomy
global $wp_taxonomies;
if(!taxonomy_exists('warrant_status'))
    $wp_taxonomies['warrant_status'] = 'delete';

// The normal loop
$args=array(
        'post_type' => $type,
        'posts_per_page' => 6,
        'paged' => $paged,
        'tax_query' =>
                array(
                        array(
                                'taxonomy' => 'warrant_status',
                                'field' => 'id',
                                'terms' => 'most-wanted',
                        )
                )

        );                                       

$my_query = null;
$my_query = new WP_Query($args);

// After finish the loop, remove the hack
// do the if to don't delete a real taxonomy
if ($wp_taxonomies['warrant_status'] == 'delete')
    unset($wp_taxonomies['warrant_status'])
share|improve this answer

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.