Add in your theme functions.php the code below
add_filter( 'shortcode_atts_adverts_list', 'my_adverts_list_category_exclude_atts', 10, 3 );
function my_adverts_list_category_exclude_atts( $out, $pairs, $atts ) {
if( ! isset( $atts["_category_exclude"] ) ) {
$atts["_category_exclude"] = null;
}
$out["_category_exclude"] = $atts["_category_exclude"];
return $out;
}
add_filter( "adverts_list_query", "my_adverts_list_category_exclude", 10, 2 );
function my_adverts_list_category_exclude( $args, $params ) {
if( isset( $params["_category_exclude"] ) ) {
if( ! isset( $args["taxonomy"] ) || ! is_array( $args["taxonomy"] ) ) {
$args["tax_query"] = array();
}
$args["tax_query"][] = array(
'taxonomy' => 'advert_category',
'field' => 'term_id',
'terms' => $params["_category_exclude"],
'operator' => 'NOT IN',
);
}
return $args;
}
Then you will be able to use the [adverts_list] as
[adverts_list _exclude_category="50" allow_sorting="1" order_by="date-desc" ]
This will list Ads from all categories except the category with ID = 50.
Ok thank you for this. We gave this a go, function in the child themes functions.php file and it does not seem to work, the category was still showing in the list with the page implementing:
[adverts_list _exclude_category=”50″ allow_sorting=”1″ order_by=”date-desc” ]
Our example:
Motor Section (id = 60)
– Motor Cars (id = 50)
– Motor Commercial (id= 55)
We just want to exclude both the Motor categories (50, 55) from the All Custom [adverts_list].
Thanks
J
I am testing this function right now and it seems to be working fine for me.
Before the line return $args; add
echo "<pre>";
print_r($args);
print_r($params);
echo "</pre>";
refresh the page you should see the debug output, copy it and paste here.
Ok here is the debug output..
Array
(
[author] =>
[post_type] => advert
[post_status] => publish
[posts_per_page] => 20
[paged] => 1
[s] =>
[meta_query] => Array
(
)
[tax_query] =>
[orderby] => Array
(
[date] => DESC
)
)
Array
(
[name] => default
[author] =>
[redirect_to] =>
[search_bar] => enabled
[show_results] => 1
[category] =>
[columns] => 3
[display] => grid
[switch_views] => grid
[allow_sorting] => 1
[order_by] => date-desc
[paged] => 1
[posts_per_page] => 20
[show_pagination] => 1
[_category_exclude] =>
[list_type] => all
)
Use the shortcode as
[adverts_list _category_exclude="50" allow_sorting="1" order_by="date-desc" ]
In the example i posted earlier there is param _exclude_category instead of _category_exclude.
ah yea easy to miss .. So that works now. Is it possible to exclude two categories or is it limited to excluding one category?
Thanks
J
You can change the line 'terms' => $params["_category_exclude"], to
'terms' => array_map( "absint", explode( ",", $params["_category_exclude"] ) ),
then in the shortcode list the excluded categories separated by coma, like this
_category_exclude="50,100,150"
Hi
Thank you for this much appreciated..
Thanks
J