Limited Time Offer - Save 10%

Post Category Archive


This document explains how to use the custom query option available in the Divi FilterGrid plugin to display the current post category results. We are also using the custom filters option to display any child categories of the current post and post tags as filters in the grid.

The first step is to add the Divi FilterGrid module to the category archive pages. To do this, log into your WordPress site and go to Divi -> Theme Builder. Click on the Add New Template box and then select the All Category Pages option in the popup and then the Create Template button at the bottom of the popup.

Next click on the Add Custom Body box and then Build Custom Body in the next popup. You should now see the standard Divi Page Builder options that appear when creating a new page. Create the layout you want and add the Divi FilterGrid module to the layout.

Configure the Divi FilterGrid module however you want as usual. There are two options we need turn on. In the Query Options section, choose the Custom Query option under the Query Type dropdown. In the Filters Options, turn on the Use Custom Filters option. These two options are required to enable our custom query and custom filters.

Before we save our module, we need to give it a special CSS ID, which we’ll then use in our functions below to target this specific module. Go to the module’s Advanced tab and give the module the CSS ID dfg-post-category.

Next we need to save our module, save our template, go back to the Divi Theme Builder page, and click the Save Changes button in the top left corner. The Divi FilterGrid module will now load on every post category archive page. But we still need to tell the module which posts and filters to load.

The last step is to add the custom query and filters functions and filters to your functions.php file located in your child theme.

Copy and paste the code below to your functions.php file:

// DFG CUSTOM QUERY - POST CATEGORY ARCHIVE PAGE
function dpdfg_custom_query($query_args, $props) {
	// GET THE ID OF THE CURRENT CATEGORY
    $wp_queried_object_id = $props['the_ID'];
	// ONLY USE THIS CUSTOM QUERY ON MODULES WITH ID "dfg-post-category"
    if (isset($props['module_id']) && $props['module_id'] == "dfg-post-category") {
        return array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => 12,
            'tax_query' => array(
                array(
                    'taxonomy' => 'category',
                    'terms' => array($wp_queried_object_id),
                    'field' => 'term_id',
                    'operation' => 'IN',
                    'include_child' => true
                )
            )
        );
    } else {
	// IF MODULE ID IS NOT "dfg-post-category", USE THE QUERY ARGUMENTS FROM THE MODULE SETTINGS
        return $query_args;
    }
}
add_filter('dpdfg_custom_query_args', 'dpdfg_custom_query', 10, 2);

// DFG CUSTOM FILTERS FILTER - POST CATEGORY ARCHIVE PAGE

function dpdfg_custom_filters_taxonomies($taxonomies, $props) {
	// ONLY USE THIS CUSTOM FILTER ON MODULES WITH ID "dfg-post-category"
	if (isset($props['module_id']) && $props['module_id'] == "dfg-post-category") {
    	return 'category,post_tag';
	}
}

add_filter('dpdfg_custom_filters_taxonomies', 'dpdfg_custom_filters_taxonomies', 10, 2);

function dpdfg_custom_filters($filters, $props) {
	// GET THE ID OF THE CURRENT CATEGORY
    $wp_queried_object_id = $props['the_ID'];
	// ONLY USE THIS CUSTOM FILTER ON MODULES WITH ID "dfg-post-category"
    if (isset($props['module_id']) && $props['module_id'] == "dfg-post-category") {
        $custom_filters = array();
		// GET THE CHILD CATEGORIES OF THE CURRENT CATEGORY
		$children_terms = get_term_children($wp_queried_object_id, "category");
		// GET ALL AVAILABLE POST TAGS
		$tags = get_terms('post_tag');
		// IF CATEGORY HAS CHILD CATEGORIES OR IF TAGS EXIST, DISPLAY THE CUSTOM FILTERS.
		if(!empty($children_terms) || !empty($tags)) {
			// ADD THE DEFAULT ALL FILTER WHICH DISPLAYS ALL RESULTS
			$custom_filters[] = array('id' => "all", 'name' => 'ALL');
			// IF CATEGORY HAS CHILD CATEGORIES, ADD EACH CHILD CATEGORY TO THE CUSTOM FILTERS ARRAY
			if(!empty($children_terms)) {
				foreach ($children_terms as $term) {
					$term_object = get_term($term);
					$custom_filters[] = array(
						'id' => $term,
						'name' => $term_object->name
					);
				}
			}
			// IF TAGS EXIST, ADD EACH TAG TO THE CUSTOM FILTERS ARRAY
			if(!empty($tags)) {
				foreach ($tags as $tag) {
					$custom_filters[] = array(
						'id' => $tag->term_id,
						'name' => $tag->name
					);
				}
			}
		}
        return $custom_filters;
    } else {
        return $filters;
    }
}
add_filter('dpdfg_custom_filters', 'dpdfg_custom_filters', 10, 2);

Pin It on Pinterest

Share This