Support » Developing with WordPress » Access table `wp_termmeta` with WP_query

  • El Asador Aleman

    (@el-asador-aleman)


    Hi folks!

    I am trying to perform a search using WP_Query. To give the search more performance I decided to use meta_tags on attachments. When the image is uploaded the action add_meta_tags writes a couple of meta tags in the table wp_termmeta. Works fine, even updating those meta tags works fine.

    Now I am trying to run a search using WP_query which looks like this:

    
    // Query Args
        $args = array(
     
            /* Post params */
     
            'post_status' => 'inherit',
            'post_type' => 'attachment',
     
            /* Meta key params */
     
            'meta_key' => 'country',
            'meta_compare' => '=',
            'meta_value' => 'chile',
     
            /* Sorting params */
     
            'order' => 'desc',
            'orderby' => 'date',
     
            /* Pagination params */
     
            'nopaging' => false,
            'posts_per_page' => 10,
            'ignore_sticky_posts' => false,
        );
     
        // The Query
        $the_query = new WP_Query( $args );
    

    I thought that declaring the post_type as attachment would tell WP_Query to look in the table wp_termmeta, but that’s not happening. WP_Query is runing the meta_query on the table table wp_postmeta which is not what I want.

    Any suggestions. Tried a lot of stuff, but seems that I am overseeing something here.

    [Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    WP_Query does not offer arguments for term meta. You can add taxonomy term arguments by using “tax_query”, but this accesses terms, not term meta. To utilize term meta arguments in a posts query, you would need to build your own SQL query string. Either use $wpdb methods to run your query, or use the various query filters available in WP_Query::get_posts() to alter the SQL that WP creates.

    By default, attachments do not support tags, categories, or any taxonomy. The terms are assigned to the attachment’s parent post. Unless a custom relationship has been established, any terms you’ve added are assigned to the parent post, not the attachment. This relationship needs to be recognized as you build your query string.

    El Asador Aleman

    (@el-asador-aleman)

    Hello bcworkz! Thanks for your reply.

    Will do so. What do you recommend, $wpdb methods or the query filters?

    I was wondering what is the purpose of wp_termmeta then? I don’t really get it.

    Moderator bcworkz

    (@bcworkz)

    Hmmm, I think I would use $wpdb methods myself. The code ought to be a little more robust. I’m not too keen on altering queries by string manipulation, which is what would happen in filters. Plus you’d need to ensure you are altering the correct query. When you call your own code there is little doubt.

    Even if term meta is not easily queryable, it’s still a good place to store term related data. It broadens what you can use taxonomies for. I expect some day term meta query arguments will be added. Term meta is a relatively recent added feature after all. In the mean time, writing your own SQL is always a viable option.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Access table `wp_termmeta` with WP_query’ is closed to new replies.