Filter based on custom field

Hey there,
I have a CPT called “Communities” and another one called “Foro”. These CPTs have a bidirectional relationship and “communities” have another relationship with the users.
I have a query loop of the posts in “foro” with a meta query like this:

image

The meta key is the relationship of the “foro” to the “communities” and then I have a dynamic tag as the value for the User relationship to the “Communities”. This allows me for the user to only see the posts in the “foro” that have the same relationship to the “communities” as the user.

Now… If a user is related to more than 1 community, I have a filter for that custom field (relationship foro>communities), but since I had to give all my relationship field a return value of the post ID, the filter only gives me the communities post IDs. I need to have it return post ID or else the query loop won’t work as intended.

image

Is there a way for the filter to give me the communities Title instead of the ID? I know I can replace the labels, but that would mean to return to the filter any time my client creates a new “community”.

I hope what I said was clear!

Thanks

There is a way if you are comfortable using php with this filter


add_filter('bricks/element/settings', function ($settings, $element) {

  if ($element->name !== 'filter-select') return $settings;

  if ($element->id !== 'kwhsqe') return $settings;

  // Use a WP Query here and use results in for-loop to create option labels

  $settings['customLabelMapping'] = [
    [
      'id' => Bricks\Helpers::generate_random_id(false), // generate a random ID, set to false to prevent echoing the ID
      'optionMetaValue' => '1043',
      'optionLabel' => 'Test automatic: 1043',
    ],
    [
      'id' => Bricks\Helpers::generate_random_id(false),
      'optionMetaValue' => '48',
      'optionLabel' => 'Test automatic: 48',
    ]
  ];

  return $settings;
}, 10, 2);

This modifies the settings for this particular select field and adds Labels to corresponding MetaValues. You can modify this to query for your Post-type and programmatically add the label.

Hope this Helps
Cheers Suat

1 Like

Hey @SuatB!

I’m trying to get this to work, but Bricks seems to ignore the customLabelMapping when edited directly from within this hook!

print_r($settings) before returning them:

After saving the page, and updating the index, no label is changed:
image

Any help would be highly appreciated!

@Matej Could this be a bug?

Hello @frussane,

this should work. But, the values that we compare should be a string - so I believe if you wrap your $post_id to strval( $post_id ), it should work.

Can you test with the line looking like this: 'optionMetaValue' => strval( $post_id ),

Let me know :slight_smile:

Thank you.
Matej

1 Like

Hello @Matej

Thank you for your input, that has indeed solved the problem, I hadn’t actually thought of that!

Thank you, have a great day :grin:

1 Like

Perfect, I’m happy that it works :wink:

Have a nice day you too!
Matej

1 Like