Orangeman555
Orangeman555

Reputation: 1181

Wordpress - Custom taxonomy no results in some cases. Need to perfect query

Eventually I'm going to have to fall back to writing custom queries for tags and categories in my custom post type, but for now I was rather hoping that I could do it the wordpress way.

Problem: If you look at my site (bottom of post) some categories show results, some don't. What I've attempted is to get parent categories to even show their children, allowing a sort of taxonomy drill-down of image results, refining by category. Maybe its over ambitious? It seemed reasonable.

Here is the code that builds up the query:

if ( is_tax( 'image-type' ) ) {

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );            
    $term_ID = $term->term_taxonomy_id;

    $children = get_term_children( $term_ID, 'image-type' );
    array_push($children, $term_ID);

    $tax_query = array(
         array(
             'taxonomy' => 'image-type',
            'field' => 'id',
            'terms' => $children,

        ) 
    );
}

This sets up part of query for parents and children.

the query:

if ( get_query_var('paged') )

    $paged = get_query_var('paged');

elseif ( get_query_var('page') )

    $paged = get_query_var('page');

else
    $paged = 1;


$local_query = array(  
    'post-type' => 'image',
    'paged' => $paged,
    'tax_query' => $tax_query,

);

Trouble site 1: http://www.clipartillustrations.com/image-type/all-illustrations-images/

Trouble Site 2: http://photominingstock.com/earth/

Both of these are testing sites. If you look at the image pages, you can see many of them indeed are in the categories, but they are not getting displayed on the category page.

Things were working reasonably well until I discovered that taxonomies cannot share like terms or one will give a 404. I tried to remedy it this way, which seems to have gone well but may have given me problem above:

First question >> Custom Taxonomy Slug Hook?

Fix one prob, make another.

The whole thing may possibly be solved in a more intelligent query - so hopefully I can wake up to a more qualified person showing me the way it should be written :D.

Upvotes: 0

Views: 792

Answers (1)

Orangeman555
Orangeman555

Reputation: 1181

Solution:

Two lines needed adjustment:

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );            
$term_ID = $term->term_taxonomy_id;

Revised and working:

if ( is_tax( 'image-type' ) ) {

    $term = get_term_by( 'slug', get_query_var( 'image-type' ),  'image-type' );            
    $term_ID = $term->term_id;

    $children = get_term_children( $term_ID, 'image-type' );
    array_push($children, $term_ID);

    $tax_query = array(
         array(
             'taxonomy' => 'image-type',
            'field' => 'id',
            'terms' => $children,

        ) 
    );
}

Upvotes: 0

Related Questions