DEV Community

loading...
Cover image for WP Snippet #010 Filter posts by (Acf) meta values

WP Snippet #010 Filter posts by (Acf) meta values

Stephan Nijman
Hi I'm Stephan Nijman! WordPress, Php, Javascript, React and Css Developer/Enthusiast/Blogger/Youtuber.
Originally published at since1979.dev ・3 min read

Originally posted on my website on March 24th 2020

Getting a list of posts filtered by meta value

Let's say we have a tech blog where we have posts talking about different tips for specific operating systems. Of course we could use taxonomies to categorize these posts by Os. But if for some reason we need to use custom meta fields we will need a way to filter our posts. In these situations we can use meta queries like shown below.

Get posts by single meta value

So we have a list of posts all with a custom field named "Os". I used Acf to create a custom select field but this wil also works with native custom meta fields. Now we want to list all posts that are about "MacOs". For this we can use the code snippet below.

Here we first create a new array called $args. This $args array consist of two keys. The first one is post_type we want to select. In our case we want the posts so we set this to post. This could of course also be a custom post type.

Next we set a key/value pair for meta_query. With the meta_query argument we can start selecting posts by meta key. Meta_query accepts an array of array's each representing a specific select statement. In our case we want to select all posts where the meta field "Os" is equal to "MacOs" so we set the following key/value pairs:

  • Key (Os): The name of the meta field.
  • Value (MacOs): The value the meta key should have.
  • Compare (=): The comparison operator to use.

Next we use the get_posts function and pass it our $args array. If all is well this should retrieve all posts where MacOs is the Os. And finaly we simply print out the results.

Note: When using the get_posts function we can also specify array keys for numberposts, category, include, exclude and suppress_filters. See the docs here.

Get posts by meta list value

If we want to show all the posts from both MacOs and Linux we can change our meta_query array like shown below.

Here instead of passing a string to the value key we pass an array containing the possible values we want to select. In our case we pass MacOs and Linux. For this to work we also have to change the compare key to 'IN'.

The get_posts function should now get all the posts that have either a meta value of MacOs or Linux.

More to come

As you can see filtering posts with the meta_query can be pretty powerful and these code snippets only scratched the surface of what we can do. I will publish some more snippets on this topic soon.

Follow

Found this post helpful? Follow me on twitter @Vanaf1979 or here on Dev.to @Vanaf1979 to be notified about new articles, and other WordPress development related resources.

Thanks for reading and stay safe

Discussion (2)

Collapse
techktsocially profile image
Tech KT

Hello Stephan,

Thanks for the wonderful post, I am actually 0 in coding, and pardon me for my upcoming silly question.

Q: Where to add these? Will adding in functions.php works?

Please suggest.

Thanking in anticipation.

Prat

Collapse
vanaf1979 profile image
Stephan Nijman Author

Hi Prat,

There are many ways you could use this technique. This specific example is meant to be used inside a template file.

What are you trying to achieve? Maybe i can help you figure it out! :)

Cheers,

Stephan