1

I'm using the Mini Loops plugin to display recent posts. When viewing a parent category it displays the posts from that category as well as any child categories. I would like to change this so only the posts assigned to the parent category are displayed.

After looking through the documentation for the plugin, I think it might be possible to use a filter to modify the plugin's output but I'm not sure exactly how to do it.

The following function is given in the documentation as an example of modifying the query based on tags:

/*
Modify the query args before they are run
Can either work on all instances, or only some by checking existing args

This demo changes the 'tag__in' query to 'tag__and' to limit the number of matching posts
*/
add_filter( 'miniloops_query' , 'miniloops_query_filter_test' );
function miniloops_query_filter_test( $query ) {
    if ( $query['tag__in'] == array( 53, 82 ) ) {
        $query['tag__and'] = $query['tag__in'];
        unset( $query['tag__in'] );
    }
    return $query;
}

The function modifies the query argument but if the argument doesn't already exist can it be added rather than just modified? Not sure if this is even possible.

UPDATE: I added below what I think is the relevant section of code in the plugin file:

$tax_query = array();

foreach( array_keys( $taxes ) as $k => $slug ) {

    //original code is this single commented line, if you need to revert

    //$tax_query[] = array( 'taxonomy' => $slug, 'field' => 'id', 'terms' => explode( ',', $taxes[ $slug ] ) );



    $oper = 'IN';

    $ids = explode( ',', $taxes[ $slug ] );

    if ( count( $ids ) == 1 && $ids['0'] < 0 ) {

        //if there is only one id given, and it's negative

        //let's treat it as 'posts not in'

        $ids['0'] = $ids['0'] * -1;

        $oper = 'NOT IN';

    }

    $tax_query[] = array(

        'taxonomy' => $slug,

        'field'    => 'id',

        'terms'    => $ids,

        'operator' => $oper );

}

I tried adding include_children => FALSE but it didn't work.

  • I know how to do in without plugin, but i never used Mini Loops plugin. – Govind Kumar Dec 1 '16 at 5:15
  • @Govind Kumar Could you show me how? – jrcollins Dec 1 '16 at 5:27
  • you have to pass false for include_children inside tax_query array see this answer wordpress.stackexchange.com/a/126041/90674 – Govind Kumar Dec 1 '16 at 5:29
  • Maybe have to hardcode it into the plugin file? – jrcollins Dec 2 '16 at 11:05
  • you can put that code inside the functions.php @jrcolins – Govind Kumar Dec 2 '16 at 11:07
1

I was finally able to get this to work using the filter provided by the plugin. The query uses [cat] to get the category which doesn't support the include_children parameter. Therefore, I had to define the tax_query argument before unsetting [cat].

Here's the function:

add_filter( 'miniloops_query' , 'miniloops_child_posts_filter' );
function miniloops_child_posts_filter( $query ) {
$query['tax_query'] = array( array(
  'taxonomy' => 'category',
  'field'    => 'term_id',
  'terms'    => $query['cat'],
  'include_children' => 0,
),);
unset($query['cat']);
return $query;
}

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.