Filter Terms by a CPT Meta Query?

Hey Bricks Team,

I have created a custom post type (CPT) called “Jobs” and assigned a custom taxonomy “Industries” and a custom field “jobs_oeffentlich” to the CPT.

On the homepage, I want to display the terms or taxonomies that are assigned to the jobs. This part works fine.

However, I also have a custom field that indicates whether the job should be publicly visible in the query or only accessible via a link.

I want to exclude all jobs or categories that have the meta query “job_oeffentlich” with the value “private”.

Unfortunately, this is not working as expected. Please refer to the settings in the attached screenshot.






Hi @tobiashaas ,

Based on your screenshots, the job_oeffentlich field should be on the post instead of the term.

Can you check termmeta table if job_oeffentlich exists? If you are running a Term Query with a termmeta that doesn’t exist then the result should be empty.

Regards,
Jenn

1 Like

Thanks @itchycode

Okay, the value doesn’t exist in the term meta. Then it’s clear why nothing is displayed. But how can I implement that?

Do I need a custom query or is there a way in Bricks?

In this case, I would have to access the meta_key in the jobs somehow so that I can compare.

Hi @tobiashaas ,

Do you mean you want to have a field on the Taxonomy and indicate which term is “private” as well? If so, you need to create custom field for that taxonomy, and the field key will be added to the termmeta table.

However, if you want to query all posts with assigned terms without “private”, best to modify via bricks/posts/query_vars hook Filter: bricks/posts/query_vars – Bricks Academy

You need to retrieve a list of terms where termmeta is not ‘private’, then include them on your posts tex_query “NOT IN” + meta query jobs_oeffentlich is not ‘private’

This is just a rough concept. Hope this explanation is clear for you :slight_smile:

Regards,
Jenn

1 Like

i will try! thanks for your help :wink:

Hey @itchycode, can you tell me if this is the right way to do it?

P.S.: I moved the post to the How-To section as it fits better here.

<?php

add_filter('bricks/terms/query_vars', function($query_vars, $settings, $element_id) {
    if ($element_id !== 'vaonjt') {
        return $query_vars;
    }

    // Perform a custom WP_Query to get posts with the required meta key and value
    $args = [
        'post_type' => 'jobs',
        'meta_query' => [
            [
                'key' => 'job_oeffentlich',
                'value' => 'public',
                'compare' => '=',
            ],
        ],
        'fields' => 'ids', // Only get post IDs
        'posts_per_page' => -1,
    ];

    $query = new WP_Query($args);
    $post_ids = $query->posts;

    // Use the post IDs to filter the terms
    $query_vars['object_ids'] = $post_ids;

    return $query_vars;
}, 10, 3);

ID is vaonjt right?
image

I have 31 Posts with Industy = Saas and public


The Fronent is showing 78 Posts
image

Wordpress Debug Log

[22-Jul-2024 15:50:22 UTC] bricks/terms/query_vars filter called for element ID: vaonjt
[22-Jul-2024 15:50:22 UTC] Post IDs retrieved: 11962,11943,8815,7337,7313,7302,9036,9035,6524,8921,6498,9016,6481,6445,6464,6462,6301,6447,6438,6440,6434,6427,6425,6412,6402,6396,6371,6361,6352,6345,6336,6341,6339,6331,6329,6322,6319,6313,6312,6310,6297,6295,6288,6279,6264,6256,6254,6251,8924,6237,6202,6179,5832
[22-Jul-2024 15:50:22 UTC] Modified query vars: Array
(
[taxonomy] => Array
(
[0] => industry
)

[orderby] => count
[order] => desc
[number] => 25
[paged] => 1
[original_offset] => 0
[object_ids] => Array
(
[0] => 11962
[1] => 11943
[2] => 8815
[3] => 7337
[4] => 7313
[5] => 7302
[6] => 9036
[7] => 9035
[8] => 6524
[9] => 8921
[10] => 6498
[11] => 9016
[12] => 6481
[13] => 6445
[14] => 6464
[15] => 6462
[16] => 6301
[17] => 6447
[18] => 6438
[19] => 6440
[20] => 6434
[21] => 6427
[22] => 6425
[23] => 6412
[24] => 6402
[25] => 6396
[26] => 6371
[27] => 6361
[28] => 6352
[29] => 6345
[30] => 6336
[31] => 6341
[32] => 6339
[33] => 6331
[34] => 6329
[35] => 6322
[36] => 6319
[37] => 6313
[38] => 6312
[39] => 6310
[40] => 6297
[41] => 6295
[42] => 6288
[43] => 6279
[44] => 6264
[45] => 6256
[46] => 6254
[47] => 6251
[48] => 8924
[49] => 6237
[50] => 6202
[51] => 6179
[52] => 5832
)

)
<?php

add_filter('bricks/terms/query_vars', function($query_vars, $settings, $element_id) {
    // Debugging: Log the function execution
    error_log('bricks/terms/query_vars filter called for element ID: ' . $element_id);

    if ($element_id !== 'vaonjt') {
        error_log('Element ID does not match. Returning original query vars.');
        return $query_vars;
    }

    // Perform a custom WP_Query to get posts with the required meta key and value
    $args = [
        'post_type' => 'jobs',
        'meta_query' => [
            [
                'key' => 'job_oeffentlich',
                'value' => 'public',
                'compare' => '=',
            ],
        ],
        'fields' => 'ids', // Only get post IDs
        'posts_per_page' => -1,
    ];

    $query = new WP_Query($args);
    $post_ids = $query->posts;

    // Debugging: Log the post IDs retrieved
    error_log('Post IDs retrieved: ' . implode(',', $post_ids));

    // Use the post IDs to filter the terms
    $query_vars['object_ids'] = $post_ids;

    // Debugging: Log the modified query vars
    error_log('Modified query vars: ' . print_r($query_vars, true));

    return $query_vars;
}, 10, 3);

Hi @tobiashaas ,

I am sorry that don’t understand how you get the 78 jobs. Is it using {query_results_count}? But it is targeting which query? I don’t see a Post query from your screenshots.

Based on the 31 items wp-admin screen, you are filtering Taxonomy Industry == SAAS, and ‘public’ is a meta query?

I can’t connect the Term Query and the Jobs count relationship based on your screenshots, sorry :frowning:

If it’s difficult to explain in text, maybe you can record a short video and send it to our help@bricksbuilder.io and include this thread URL. I will try to understand better and assist.

Regards,
Jenn

This would probably be best if I made a video. It’s really hard to explain like this.
Thank you very much in advance