WP_Query and filter by custom post type and custom taxonomy term

Support General WP_Query and filter by custom post type and custom taxonomy termResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #35194
    BellulBellul
    Participant

    I have created a custom post type (slug="knowledge") with a connected a field group. All custom fields are stored in a custom table.

    One of the custom fields is called "Info Source" with the ID "kb_infosource" (which also is the name of the table column). The type of this field is Taxonomy Advanced. And it is connected to the custom taxonomy "KB Info Source" with the slug "kb-infosource".

    Now I want to find all knowledge posts which have the "KB Info Source" term name "Report" (slug=report-sso; id=202, the id is the value stored in the custom table column). I have tried this (besides a tax_query):

    $args = array(
        'post_type'  => 'knowledge',
        'meta_key'   => 'kb_infosource',
        'meta_query' => array(
            array(
                'key'     => 'kb_infosource',
                'value'   => 202,
                'compare' => '=',
            ),
        ),
    );
    $query = new WP_Query( $args );

    But this doesn't work (but otherwise it works fine to filter with a facet created with WP GridBuilder). I guess I miss something simple.

    #35208
    Long NguyenLong Nguyen
    Moderator

    Hi,

    To query posts by field value stored in the custom table, please follow the documentation
    https://docs.metabox.io/extensions/mb-custom-table/#query-posts-with-wp_query

    #35214
    BellulBellul
    Participant

    This code works just fine:

    global $wpdb;
    $rows = $wpdb->get_results("
    SELECT
    	knowledge.ID,
    	posts.post_title
    FROM wp_xtr_knowledge knowledge
    	INNER JOIN wp_posts posts ON kunskap.ID = posts.ID
    WHERE
    	knowledge.kb_infosource = 202
    ORDER BY posts.post_date_gmt DESC
    ");
    if ($wpdb->num_rows > 0) {
    	foreach ( $rows as $row ) {
        	   echo $row->post_title . '<br>';
    	}
    }
    $wpdb->flush();
    

    But how does that query look like using WP_Query?

    #35215
    BellulBellul
    Participant

    Thanks! I saw your answer after I sent my follow up question.

    The solution you propose, will not work with Oxygen Builder > Repeater > WP_Query > Advanced, I guess? So then I can use my wpdb-solution or is it better to use wpdb together with WP_Query as you propose? Or doesn't it matter which way a go?

    #35238
    Long NguyenLong Nguyen
    Moderator

    Hi,

    In case of using Oxygen Builder, please reach out to them if you have any issues with installation, configuration, compatibility, or usage.
    Refer to our support policy https://metabox.io/support/topic/support-policy/

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