You can further refine a list of posts by filtering any custom fields that the post has for a certain set of conditions, this is possible with WP_Meta_Query, which allows you to target custom fields as known as post meta.

There are a couple of ways to use the WP_Meta Query class, the meta_query array is covered here. The array takes 4 key/value pairs…

  • key
  • value
  • compare
  • type

The key is the custom field or post meta field – the value is the value of that field, compare is a comparison operator such as =, !=, REGEXP and type is  Custom field type, default value is ‘CHAR’. See the codex linked above for other possible values.

$meta_query_args = array(
    'relation' => 'AND', // other value is 'OR'
     array(
        'key'     => 'tl_features_description',
        'value'   => 'degree',
        'compare' => 'REGEXP'
     ),

 );

 ( 'meta_query', $meta_query_args );

The array is nested with only a ‘relation‘ key passed in the outer array – this is for helping to handle multiple nested arrays – the value for ‘relation‘ defaults to ‘AND‘ but can otherwise be ‘OR‘.

So in the above array we are finding a custom field called ‘tl_features_description‘ with a value that has the word ‘degree‘ in it – this is made possible by using ‘REGEXP‘ as the value for ‘compare‘.

For multiple nested arrays…

$meta_query_args = array(
    'relation' => 'AND', // other value is 'OR'
     array(
        'key'     => 'tl_features_description',
        'value'   => 'degree',
        'compare' => 'REGEXP'
     ),

     array(
        'key'     => 'tl_driver',
        'value'   => 'remote driver required',
        'compare' => 'REGEXP'
     )

 );

 ( 'meta_query', $meta_query_args );

Now a 2nd custom field is targetted also with a regex value with the outer array relation is set to ‘AND’, so both sets of conditions need to be valid for the posts to appear. You can further next more arrays.

The meta_query can be passed into to other functions such as pre_get_posts action or to a new WP_Query

// Pre get posts example
add_action( 'pre_get_posts', 'tl_lights_category_page' );
function tl_lights_category_page( $query ) {
    if (  $query->is_main_query() && !is_admin() && is_tax('light_category') ) {
            $query->set( 'posts_per_page', '-1' );
            
            $meta_query_args = array(
                    'relation' => 'AND',
                    array(
                        'key'     => 'tl_features_description',
                        'value'   => 'degree',
                        'compare' => 'REGEXP'
                    ),

                    array(
                        'key'     => 'tl_driver',
                        'value'   => 'remote driver required',
                        'compare' => 'REGEXP'
                    )

                );
            $query->set( 'meta_query', $meta_query_args );
    }
}
// New WP_Query example
// WP_Query arguments
$args = array(
	'post_type'         => array( 'light' ),
	'order'             => 'ASC',
        'orderby'           => 'title',
        'posts_per_page'    => '-1',
	'meta_query'        => array(
        'relation' => 'AND',
            array(
                'key'     => 'tl_features_description',
                'value'   => 'degree',
                'compare' => 'REGEXP'
            ),

            array(
                'key'     => 'tl_driver',
                'value'   => 'remote driver required',
                'compare' => 'REGEXP'
            )
	),
);

// The Query
$query = new WP_Query( $args );

Share:

Leave a Reply