Home Forums Front-end Issues Filtering by custom field isn't quite working


Filtering by custom field isn't quite working

  • I’m have a little bit of an issue with the following code and I was hoping for a few pointers as I think I’m close just not quite there.

    I have the following query which without any filters works fine, it outputs the posts from the post_type ‘t_shirts’. The issue comes when using filters, the filters are stored in the array $filters for example, array(‘red’,’yellow’,’blue’);

    Some items which no longer have a value stored within the ‘colour’ field appear when shouldn’t, or for example if ‘blue’ & ‘red’ are stored in the array it won’t output anything despite there being posts with the those colour values stored within the custom field ‘colour’.

    Or another example if I just select the one colour as the filter, then all items which have a value in the field ‘colour’ regardless of whether it’s been deleted or not output, as though it’s just checking to see if there ever was a value.

    I’ve attached my code and any pointers would be greatly appreciated, I feel like I’m almost there, just missing something very obvious.

    $args = array(
    	'post_type'			=> 't_shirts',
    	'showposts' 		=> -1,
    if ($filters) {
    	$args['meta_query'][] = array(
    			'key'		=> 'colour',
    			'value' 	=> $filters,
    			'compare' 	=> 'LIKE'
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Filtering by custom field isn't quite working’ is closed to new replies.