0

For years I've been using stack overflow and I could always find an answer (or half answers that I could stitch together), but this time I really can't figure this out...

So, I have an ACF Post Object field (inicia_areas) that associates one or more posts of Custom Post Type (CPT) B to CPT A posts.

The objective is to be able to filter posts in the archive of CPT A that have CPT B posts associated to them. Nothing too fancy :)

I'm using pre_get_posts() hook, since I was able to filter posts by category this way.

For tests purposes I simplified the pre_get_posts() function and hardcoded the value in the meta_query. I then associated three CPT A posts to the post with the slug "central-de-compras" to get some results.

The code:

function wpsites_query($query)
{
    if ($query->is_main_query() && !is_admin()) {

        if (is_post_type_archive('iniciativas')) {

            // these vars return arrays from form
            global $filter_areas, $filter_temas;

            // -> Categories Filter - WORKING
            if ($filter_temas && count($filter_temas) > 0) {
                $cat_ids = [];
                foreach ($filter_temas as $temas) {
                    array_push($cat_ids, get_term_by('slug', $temas, 'category')->term_id);
                }
                $query->set('category__in', $cat_ids);
            }

            // -> ACF Filter - RETURNING NO POSTS
            if ($filter_areas && count($filter_areas) > 0) {
                $query->set(
                    'meta_query',
                    array(
                        array(
                            'key'     => 'inicia_areas',
                            'value'   => 'central-de-compras',
                            'compare' => '='
                        )
                    )
                );
            }
            $query->set('posts_per_page', 12);
        }

        return $query;
    }
}
add_action('pre_get_posts', 'wpsites_query');

The if($filter_areas... is returning true, so the query is being set. I tried the values with "post-name" and "ID" and compare with "=", "LIKE", "==", "EXIST". Converted the value to an array and use the "IN" compare, but nothing. Zero results.

I didn't post the query output because TLDR, but if it helps, I'll post it.

Thanks in advance! Pedro

Pedro
  • 3
  • 3
  • Tested inside the archive using "new WP_Query", with value as ID (converted my ACF field to return ID only) and can get some results with compare "LIKE". Like is prone errors, so not a solution. Compares "=" and "IN" (-> changed my value to an array) still returns zero... – Pedro Jun 15 '23 at 15:35
  • If you call `get_post_meta( $post_id, 'incia_areas', true )` on one of the posts you'd expect back, what's the value? – Caleb Jun 15 '23 at 15:48
  • If I use the compare "LIKE", it returns array(1) { [0]=> string(4) "7914" }. ie, the hardcoded ID. Even changing the ACF field to post object, it only returns the IDs. Interesting... I did replicate this in the pre_get_posts and it worked as expected. Nice one :) So I guess that, for an array I'll have to loop each value (same key) in pre_get_posts, correct? – Pedro Jun 15 '23 at 16:24
  • I'm confused: what is the output of `get_post_meta( $post_id, 'incia_areas', true )`? – Caleb Jun 15 '23 at 17:04
  • @Caleb `array(1) { [0]=> string(4) "7914" }` – Pedro Jun 15 '23 at 18:54

1 Answers1

0

The meta_query is checking inicia_areas meta is equal to central-de-compras, but the meta data's value is an array of integers, which is why the query does not return any results. Adjust your meta data or your query so that the types match or the logic expects the correct type.

Caleb
  • 1,058
  • 1
  • 7
  • 18