• Resolved timonw

    (@timonw)


    I’ve got a custom post type (book) and a taxonomy (person). The post type has two different custom fields of the type “advanced taxonomy”, both with the same taxonomy “person”. One field is called “published” and one “has_text”. Both fields have the same taxonomy so that they share the same pool of persons. In both fields there can be chosen more than one taxonomy term.

    In my wp query I don’t know how to deal with it. On “site.com/person/max” I would like to show all books that habe the term “max”(id: 10) in the field “published”. I tried two different ways. Both don’t work.

    $args = [
        'posts_per_page' => -1,
        'meta_query' => [],
        'post_type' => 'book',
        'order' => 'DESC',
        'orderby'=> 'date',
        'tax_query' => array(
              array (
                  'taxonomy' => 'published',
                  'field' => 'term_id',
                  'terms' => '10',
              )
          ),
      ];
    'meta_query' => array( array(
            'key' => 'published',
            'value' => '10',
            'compare' => 'IN',
            'type' => 'term_id',
    ))

    The first one shows all books, the second one just the books that ONLY have the term_id 10, not those which also have other publishers.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Anh Tran

    (@rilwis)

    Hi,

    As you use the taxonomy_advanced field, the value of terms will be saved in custom fields. So you can’t use tax_query in this case. Only meta_query works.

    I think the 2nd query is close. Please try this:

    'meta_query' => [
        [
            'key' => 'published',
            'value' => '10',
        ],
    ],
    

    This will show all books that have a custom field published with value 10, which means getting all books published by “max” in this case.

    The problem is that if you choose multiple publishers, then the value is like 10,11,15 (comma-separated string), then using the above query only get books belong to one publisher.

    In this case, I’d suggest using compare param to make WordPress expand the operation. By default (when obmiting like above, it’s =). We can use LIKE as follows:

    Using LIKE:

    'meta_query' => [
        [
            'key' => 'published',
            'value' => '10',
            'compare' => 'LIKE',
        ],
    ],

    This will match 10, 10,11,15 (correct) but also match 100, 11,101,12 (incorrect). So, after getting those books, you need to check the value again, like this:

    $query = new WP_Query( $args ); // your query args, which includes meta query above
    while ( $query->have_posts() ) {
        $query->the_post();
        $published = rwmb_meta( 'published' );
        $published_ids = wp_list_pluck( $published, 'term_id' );
        if ( in_array( 10, $published_ids ) ) {
            // Output book here
        }
    }

    This is not optimal, but still does the job. You just get more books than you needs in some case, but you can verify them later.

    WP_Query also supports REGEXP in compare parameter, but I haven’t used it. If you know it with MySQL (here is a tutorial I found), then you might want to try it.

    Thread Starter timonw

    (@timonw)

    Thank you. That solved the problem.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.