• Resolved ldekay

    (@ldekay)


    Is it possible to filter the links displayed by a combination of categories, such as displaying links ONLY if they are assigned ALL of the categories specified in the AND statement (catid1+catid2). The current shortcode is essentially an OR statement. Maybe this could even work with the current OR configuration (catid1+catid2, catid3)

    I realize separate categories can be created to effectively show only the desired links, but an AND equivalent would greatly limit category overhead.

    https://www.ads-software.com/plugins/simple-links/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Mat Lipe

    (@mat-lipe)

    HI IdeKay,

    You may achieve this with this handy code snippet

    add_filter( 'simple_links_parsed_query_args', 'sl_require_all_cats' );
    function sl_require_all_cats( $query_args ){
    	if( !empty( $query_args[ 'tax_query' ] ) ){
    		foreach( $query_args[ 'tax_query' ][ 0 ][ 'terms' ] as $cat_id ){
    			$tax[] = array(
    				'taxonomy' => 'simple_link_category',
    				'fields'   => 'id',
    				'terms'    => $cat_id
    			);
    		}
    
    		$tax[ 'relation' ] = 'AND';
    		$query_args[ 'tax_query' ] = $tax;
    	}
    
    	return $query_args;
    }
    Thread Starter ldekay

    (@ldekay)

    Thanks Mat,
    I’m still learning how to do this stuff, so where EXACTLY do I put your handy code snippet? I’ve seen many references to adding code snippets and knew I’d someday have to ask this dumb question, so you get the honor of further educating me.
    Thanks again, Lloyd

    Plugin Author Mat Lipe

    (@mat-lipe)

    Hi Lloyd,

    You should be able to add it to your themes functions.php file.

    Let me know if you have other questions

    Thread Starter ldekay

    (@ldekay)

    I added it to functions.php in a child theme and it works well.

    However, now there isn’t an OR option. Is it possible to choose between AND and OR options interactively, or is it necessary to hardcode one or the other?

    Thanks again for your help and patience.

    Plugin Author Mat Lipe

    (@mat-lipe)

    Hi Lloyd,

    I have release version 2.7.3 just now which allows for custom args.

    If you adjusted your code to this

    add_filter( 'simple_links_parsed_query_args', 'sl_require_all_cats', 2, 2 );
    function sl_require_all_cats( $query_args, $factory ){
    	if( !empty( $factory->args['both_categories'] ) && !empty( $query_args[ 'tax_query' ] ) ){
    		foreach( $query_args[ 'tax_query' ][ 0 ][ 'terms' ] as $cat_id ){
    			$tax[] = array(
    				'taxonomy' => 'simple_link_category',
    				'fields'   => 'id',
    				'terms'    => $cat_id
    			);
    		}
    
    		$tax[ 'relation' ] = 'AND';
    		$query_args[ 'tax_query' ] = $tax;
    	}
    
    	return $query_args;
    }

    You will be able to specify which shortcodes need both by adding a both_categories=”1″ to your shortcode

    Like so

    [simple-links category="Ecom,Social Media" both_categories="1"]

    Thread Starter ldekay

    (@ldekay)

    Hi again Mat,

    Back onto this issue of using multiple categories to control posting of Simple Links. I just checked the website I used your script on last year and now the multiple categories technique (both_categories=”1″) doesn’t display anything.

    If I try to select two categories from the dialog that creates a Simple Links shortcode I get a strange mix of all the links that have the either category.

    I suspect you made changes in more recent plugin updates that broke the fix you gave me last year. Is there a way to recover this multi-category ability?

    Thread Starter ldekay

    (@ldekay)

    Never mind, it seems to be OK when I specify orderby=”date” instead of by “menu-order”, and I can work with that.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘AND filter by multiple categories?’ is closed to new replies.