Support » Plugin: Facetious » Only show top level categories?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Ok, I used this and was able to change the text in the dropdown. However, I still need to figure out how to exclude sub categories from appearing…

    <?php
    	do_action( 'facetious', array(
        'submit' => 'Search',
        'fields' => array(
            's',
            'category' => array(
    		'label' => 'Filter:',
    		'all'   => 'Search All Sites'
    		)
    	)
    ) );?>

    I would have thought something like
    'depth'=>1

    would work…

    Plugin Author John Blackbourn

    (@johnbillion)

    Hi Blake,

    Facetious doesn’t pass any extra parameters on to the function that fetches the terms (get_terms()) so an argument like depth=>1 won’t do anything. It’s an interesting point though and sounds like something that Facetious should definitely support. I’ll look at adding it into a future version.

    For now, you can use the ‘options’ argument and pass your own list of categories:

    <?php
    $category_options = array();
    
    foreach ( get_terms( 'category', array( 'depth' => 1 ) ) as $category )
    	$category_options[$category->slug] = $category->name;
    
    do_action( 'facetious', array(
        'submit' => 'Search',
        'fields' => array(
            's',
            'category' => array(
    		'label'   => 'Filter:',
    		'all'     => 'Search All Sites',
    		'options' => $category_options
    		)
    	)
    ) );?>

    Hope this helps.

    John

    Hi John,

    Thanks so much for your response. That may be a good short term workaround. I am a little confused, would I need to change part of that to my situation?

    This is definitely something I am willing to fund development of. If you are interested, let me know. If not, I have a PHP developer I can hire to give it a go.

    Thanks!
    Blake

    Plugin Author John Blackbourn

    (@johnbillion)

    Hi Blake,

    The ‘options’ parameter in Facetious was designed for this situation, where you need to customise the list of options displayed. It’s not just a short term workaround.

    It looks like the argument you want for get_terms() is ‘parent=>0’, not ‘depth=>1’. So the code below should work for you as-is:

    <?php
    $category_options = array();
    
    foreach ( get_terms( 'category', array( 'parent'=> 0) ) as $category )
    	$category_options[$category->slug] = $category->name;
    
    do_action( 'facetious', array(
        'submit' => 'Search',
        'fields' => array(
            's',
            'category' => array(
    		'label'   => 'Filter:',
    		'all'     => 'Search All Sites',
    		'options' => $category_options
    		)
    	)
    ) );?>

    Thanks John, that worked beautifully.

    This is a really great plugin, thanks for making it available to the community!

    Hi John,

    Is this a filter to modify the normal Facetious sidebar widget? If not, is there a way?

    Thanks,
    Blake

    Plugin Author John Blackbourn

    (@johnbillion)

    There isn’t a filter on the Facetious widget, no. What are you trying to alter? We might look at adding in some filters if necessary.

    We ended up just adding this to our functions file to create a new widget that had the different settings than the other normal one.

    class FacetiousExtend extends WP_Widget {
    
    	function FacetiousExtent() {
    		// Instantiate the parent object
    		parent::__construct( true, 'SearchSites' );
    	}
    
    	function widget( $args, $instance ) {
    		echo '<div id="facetious_widget-custom" class="widget widget_categories"><h3>Search</h3>';
    		$category_options = array();
    
    	foreach ( get_terms( 'category', array( 'parent'=> 0) ) as $category )
    		$category_options[$category->slug] = $category->name;
    
    	do_action( 'facetious', array(
    		'submit' => 'Search',
    		'fields' => array(
    			's',
    			'category' => array(
    			'label'   => ' ',
    			'all'     => 'Search All Blogs',
    			'options' => $category_options
    			)
    		)
    		)
    	);
    	echo '</div>';
    	}
    
    	function update( $new_instance, $old_instance ) {
    		// Save widget options
    	}
    
    	function form( $instance ) {
    		// Output admin widget options form
    	}
    }
    
    function modify_facetious_search() {
    	register_widget( 'FacetiousExtend' );
    }
    
    add_action( 'widgets_init', 'modify_facetious_search' );

    It isn’t perfect and the widget doesn’t show the title on the backend but it worked.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Only show top level categories?’ is closed to new replies.