WordPress.org

Ready to get started?Download WordPress

Forums

CPT-onomies: Using Custom Post Types as Taxonomies
tax_query not working (4 posts)

  1. sunriseweb
    Member
    Posted 1 year ago #

    First off - great plugin! My problem is that tax_query isn't working for me (WIP site is http://sar.sunriseweb.ca ). I checked the database and the term_relationships table doesn't contain any entries linking the object_id (i.e. post_id for my custom post type) and term_taxonomy_id (i.e. the cpt-onomy taxonomy post_id).

    So... when I try the following query I get no results:

    $postQueryArgs = array(
                          'post_type'       => 'contacts',
                          'nopaging'        => true,
                          'posts_per_page'  => -1,
                          'orderby'         => 'title',
                          'order'           => 'ASC',
                          'tax_query'       => array(
                              array(
                                  'taxonomy' => 'countries',
                                  'field'    => 'id',
                                  'terms'    => $termone->term_id,
                              )
                          )
                       );
    
      $postquery = new WP_Query( $postQueryArgs );

    Looking at the generated SQL I can see this is because there is no data in the term_relationships table. Any ideas why?

    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
    FROM wp_posts
    INNER JOIN wp_term_relationships ON ( wp_posts.ID = wp_term_relationships.object_id )
    INNER JOIN wp_postmeta AS cpt_onomy_pm1 ON ( wp_posts.ID = cpt_onomy_pm1.post_id
    AND cpt_onomy_pm1.meta_key =  '_custom_post_type_onomies_relationship' )
    WHERE 1 =1
    AND (
    wp_term_relationships.term_taxonomy_id
    IN ( 95 )
    )
    AND wp_posts.post_type =  'contacts'
    AND (
    wp_posts.post_status =  'publish'
    OR wp_posts.post_status =  'future'
    OR wp_posts.post_status =  'draft'
    OR wp_posts.post_status =  'pending'
    OR wp_posts.post_status =  'private'
    )
    AND (
    cpt_onomy_pm1.meta_value
    IN ( 95 )
    )
    GROUP BY wp_posts.ID
    ORDER BY wp_posts.post_title ASC
    LIMIT 0

    What I'm doing to work around this is using a meta_query, and this is working for now:

    $postQueryArgs = array(
                      'post_type'       => 'contacts',
                      'nopaging'        => true,
                      'posts_per_page'  => -1,
                      'orderby'         => 'title',
                      'order'           => 'ASC',
                      'meta_query' => array(
                           array(
                               'key' => '_custom_post_type_onomies_relationship',
                               'value' => array(intval($_POST['countryid'])),
                               'compare' => 'IN',
                           )
                       )
                   );
    $postquery = new WP_Query( $postQueryArgs );

    Thanks in advance.

    http://wordpress.org/plugins/cpt-onomies/

  2. sunriseweb
    Member
    Posted 1 year ago #

    On what might be a related note, when I get_term inside a function it returns the wrong result, but works fine directly in a template:

    global $cpt_onomy;
    $termone = $cpt_onomy->get_term( 95, 'countries' );

    However the following code works fine if I put it inside the same function.

    $terms = get_terms("countries", 'orderby=name&hide_empty=0');
    $count = count($terms);
    if ( $count > 0 ){
       echo "<ul>";
       echo '<li># Countries='.$count.'</li>';
       foreach ( $terms as $term ) {
         $thisterm = $cpt_onomy->get_term( $term->term_id, 'countries' );
         echo "<li> ". $thisterm->term_id . '=' . $term->term_id . " : " . $term->name . "</li>";
    
       }
       echo "</ul>";
    }
  3. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    The reason there's no data in the term_relationships table is because CPT-onomies doesn't store relationship info the same way as regular taxonomies. As you've discovered, it stores the info as post meta so that it can simply retrieve post data via the post ID stored in the post meta.

    with that said, I've written code that allows you to use WP_Query with CPT-onomies and I just tested it in my setup and its working. What data is WP_Query outputting that's incorrect?

    If you look in the "Help" tab on the CPT-onomies settings page, and go to Troubleshooting, you'll see a section titled "When I filter or query posts, the results are incorrect". I'm wondering if that section will help you out.

    AND if my theory on your first post is correct, it will probably take care of the second issue.

    Hope this helps!

  4. Dalton Rooney
    Member
    Posted 1 year ago #

    In case anyone has the same problem, a possible solution:

    Tax_queries using CPT-onomies stopped working recently for me as well. It turns out I was missing the 'suppress_filters' => false argument in my query. It worked for over a year without that argument and suddenly stopped working, but adding it seems to have fixed the problem.

    Reference: http://wpdreamer.com/plugins/cpt-onomies/documentation/get_posts/

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic