Support Forum
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.
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
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?
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?
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/