0

Is there a more elegant way of doing this same thing. I have three filters that I want to use to effect the results in the fourth field. The pool, month, and tags are the filters. I want the tags to ignored unless a tag is selected. I kept running into the problem that if no tags were selected then results would show in the final field.

I came up with a cheap way of doing this, but I'm thinking there is a better way.

My approach is if the array for the tags is empty, then I'm changing the values of the $args variable that gets passed to the WP_Query. During that check i am setting the value of a variable called $tag_status to not_empty if a tag happens to be selected. Then I am just checking the value of that variable.

Take a look:

if($tag_status == "not_empty"){
        $args = array(
            'post_type' => 'daily-marriage-tips',
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'cat_tips',
                    'field'    => 'slug',
                    'terms'    => $get_fym_dmt_pool,
                ),
                array(
                        'taxonomy' => 'cat_months',
                        'field'    => 'slug',
                        'terms'    => array_for_query($current_post_id, $get_fym_dmt_month_arr),
                ),
                array(
                        'taxonomy' => 'tag_tips',
                        'field'    => 'slug',
                        'terms'    => array_for_query($current_post_id, $get_fym_dmt_tags_arr),
                        'relation' => 'AND'
                ),
            ),
        );
    } else {
        $args = array(
            'post_type' => 'daily-marriage-tips',
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'cat_tips',
                    'field'    => 'slug',
                    'terms'    => $get_fym_dmt_pool,
                ),
                array(
                        'taxonomy' => 'cat_months',
                        'field'    => 'slug',
                        'terms'    => array_for_query($current_post_id, $get_fym_dmt_month_arr),
                )
            ),
        );
    }

I had tried putting the tags query into an array and setting the relation to "OR", but that made the 4th field empty unless I selected a tag. I don't want to have to select a tag as that filter is a bonus filter. It's an optional thing. What would be the optimal approach?

0

Good days. I found a better approach.

I have a custom function that looks at the multi-dimensional array and determines if they are empty.

Using that I found a way to dynamically add tax_query array into the main query for my wp_query.

I kept the "pool" in the main $args. I removed the month and tag. If the user selects a month and then a tag these new array items will be added to the tax_query.

It ended up looking like this:

$args = array(
            'post_type' => 'daily-marriage-tips',
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'cat_tips',
                    'field'    => 'slug',
                    'terms'    => $get_fym_dmt_pool,
                ),
            )
        );

        // MONTHS ARRAY: check if the months array is empty
        is_array_empty($current_post_id, $get_fym_dmt_month_arr);
        if(is_array_empty($current_post_id, $get_fym_dmt_month_arr) == "not_empty"){
            array_push( 
                $args['tax_query'],
                array(
                    'taxonomy' => 'cat_months',
                    'field'    => 'slug',
                    'terms'    => array_for_query($current_post_id, $get_fym_dmt_month_arr),
                    )
             );
        }


        // TAGS ARRAY: check if the tags array is empty
        is_array_empty($current_post_id, $get_fym_dmt_tags_arr);
        if(is_array_empty($current_post_id, $get_fym_dmt_tags_arr) == "not_empty"){
            array_push( 
                $args['tax_query'],
                array(
                                'taxonomy' => 'tag_tips',
                                'field'    => 'slug',
                                'terms'    => array_for_query($current_post_id, $get_fym_dmt_tags_arr),
                         )
             );
        }

My custom function is_array_empty accepts two arguments. The post id and the array you want to check. Here is what it looks like:

// Check if the array is empty
function is_array_empty($pID, $array) {
    $item_arr = array();
    foreach($array as $item_arr) {
        if($item_arr[0] == NULL){
            $status = "empty";
        }else{
            $status = "not_empty";
        }           
    }
    return $status;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.