Solved

Combine 2 functions in wordpress

Posted on 2014-09-15
6
225 Views
Last Modified: 2014-10-24
Hello,

I've been trying to get some dropdowns in a search filter to show both post count and also heirarchy structure in the categories and taxonomies.

Both of the functions below work but not when they are together, when they are both in my functions file, only the last one works (I can swap them around and both work, but only the last one to appear in the functions).

Can anyone please advise how to combine them so they both work? I'm really up against it and need to get this working asap.


Thanks!!!

// add heirarchy to search drop downs 

add_filter('uwpqsf_taxonomy_arg', 'custom_term_output','',1);
function custom_term_output($args){
$args['parent'] = '0';
return $args;
}
//MODIFY TAXFIELD DROPDOWN OUTPUT TO IDENTIFY AND STYLE CHILD CATEGORIES
add_filter('uwpqsf_tax_field_dropdown','custom_dropdown_output','',12);
function custom_dropdown_output($html,$type,$exc,$hide,$taxname,$taxlabel,$taxall,$opt,$c,$defaultclass,$formid,$divclass){

$args = array('hide_empty'=>$hide,'exclude'=>$eid );
$taxoargs = apply_filters('uwpqsf_taxonomy_arg',$args,$taxname,$formid);
$terms = get_terms($taxname,$taxoargs); $count = count($terms);

			 if($type == 'dropdown'){
			$html  = '<div class="'.$defaultclass.' '.$divclass.' tax-select-'.$c.'"><span class="taxolabel-'.$c.'">'.$taxlabel.'</span>';
			$html .= '<input  type="hidden" name="taxo['.$c.'][name]" value="'.$taxname.'">';
			$html .= '<input  type="hidden" name="taxo['.$c.'][opt]" value="'.$opt.'">';
			$html .=  '<select id="tdp-'.$c.'" name="taxo['.$c.'][term]">';
			if(!empty($taxall)){
				$html .= '<option selected value="uwpqsftaxoall">'.$taxall.'</option>';
			}
					if ( $count > 0 ){
						foreach ( $terms as $term ) {
					 $selected = $terms[0]->term_id;
$html .= '<option value="'.$term->slug.'">'.$term->name.'</option>';

    $args = array(
        'hide_empty'    => false,
        'hierarchical'  => true,
        'parent'        => $term->term_id
    );
    $childterms = get_terms($taxname, $args);

    foreach ( $childterms as $childterm ) {
            $selected = $childterms[0]->term_id;

        $html .= "<option value='".$childterm->slug."'".">    >"  . $childterm->name . '</option>';

    }}
			}
			$html .= '</select>';
			$html .= '</div>';
			
			

			return  apply_filters( 'custom_dropdown_output', $html,$type,$exc,$hide,$taxname,$taxlabel,$taxall,$opt,$c,$defaultclass,$formid,$divclass);
		}

}



// add post count to search drop downs 


add_filter('uwpqsf_tax_field_dropdown','add_post_Cgg','',12);
function add_post_Cgg($html ,$type,$exc,$hide,$taxname,$taxlabel,$taxall,$opt,$c,$defaultclass,$formid,$divclass){
$eid = explode(",", $exc);
		$args = array('hide_empty'=>$hide,'exclude'=>$eid );	
		$taxoargs = apply_filters('uwpqsf_taxonomy_arg',$args,$taxname,$formid);
        	$terms = get_terms($taxname,$taxoargs); $count = count($terms);
 
			$html = '';
			$html  .= '<div class="'.$defaultclass.' '.$divclass.' tax-select-'.$c.'"><span class="taxolabel-'.$c.'">'.$taxlabel.'</span>';
			$html .= '<input  type="hidden" name="taxo['.$c.'][name]" value="'.$taxname.'">';
			$html .= '<input  type="hidden" name="taxo['.$c.'][opt]" value="'.$opt.'">';
			$html .=  '<select id="tdp-'.$c.'" name="taxo['.$c.'][term]">'; 
			if(!empty($taxall)){
				$html .= '<option selected value="uwpqsftaxoall">'.$taxall.'</option>';
			}
					
						foreach ( $terms as $term ) {	
						$term_obj = get_term( $term->term_id, $taxname );
					$html .= '<option value="'.$term->slug.'">'.$term->name.' ('.$term_obj->count.')</option>';}
					
			$html .= '</select>';
			$html .= '</div>';return $html;

	
}

Open in new window

0
Comment
Question by:igloobob
  • 3
6 Comments
 

Accepted Solution

by:
igloobob earned 0 total points
ID: 40325250
For those that are interested I got this working with a little help from a friend as shown below

// Ultimate WP Query Search Filter heirarchy in drop downs

add_filter('uwpqsf_taxonomy_arg', 'custom_term_output','',1);
function custom_term_output($args){
$args['parent'] = '0';
return $args;
}
//MODIFY TAXFIELD DROPDOWN OUTPUT TO IDENTIFY AND STYLE CHILD CATEGORIES
add_filter('uwpqsf_tax_field_dropdown','custom_dropdown_output','',12);
function custom_dropdown_output($html,$type,$exc,$hide,$taxname,$taxlabel,$taxall,$opt,$c,$defaultclass,$formid,$divclass){

$args = array('hide_empty'=>$hide,'exclude'=>$eid );
$taxoargs = apply_filters('uwpqsf_taxonomy_arg',$args,$taxname,$formid);
$terms = get_terms($taxname,$taxoargs); $count = count($terms);

			 if($type == 'dropdown'){
			$html  = '<div class="'.$defaultclass.' '.$divclass.' tax-select-'.$c.'"><span class="taxolabel-'.$c.'">'.$taxlabel.'</span>';
			$html .= '<input  type="hidden" name="taxo['.$c.'][name]" value="'.$taxname.'">';
			$html .= '<input  type="hidden" name="taxo['.$c.'][opt]" value="'.$opt.'">';
			$html .=  '<select id="tdp-'.$c.'" name="taxo['.$c.'][term]">';
			if(!empty($taxall)){
				$html .= '<option selected value="uwpqsftaxoall">'.$taxall.'</option>';
			}
					if ( $count > 0 ){
						foreach ( $terms as $term ) {
            $term_obj = get_term( $term->term_id, $taxname );
					 $selected = $terms[0]->term_id;
$html .= '<option value="'.$term->slug.'">'.$term->name.' ('.$term_obj->count.' posts)</option>';

    $args = array(
        'hide_empty'    => false,
        'hierarchical'  => true,
        'parent'        => $term->term_id
    );
    $childterms = get_terms($taxname, $args);

    foreach ( $childterms as $childterm ) {
            $selected = $childterms[0]->term_id;
            $term_obj = get_term( $childterm->term_id, $taxname );
        $html .= "<option value='".$childterm->slug."'"."> &nbsp;&nbsp; - &nbsp;"  . $childterm->name . ' ('.$term_obj->count.' posts)</option>';

    }}
			}
			$html .= '</select>';
			$html .= '</div>';
			
			

			return  apply_filters( 'custom_dropdown_output', $html,$type,$exc,$hide,$taxname,$taxlabel,$taxall,$opt,$c,$defaultclass,$formid,$divclass);
		}

}

Open in new window

What I'd now like to do is hide empty categories as even with that selected in the plugin settings, they are still being shown.

I'd also like to know how change this to hide the child categories when desired as an option, yet include those child category posts in both the parent category post count and search results.
0
 

Author Comment

by:igloobob
ID: 40325269
Ok, realised the hiding empty categories bit

'hide_empty'    => true,

Open in new window


d'oh!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40326773
so close the question accepting your own answer
0
 

Author Comment

by:igloobob
ID: 40326792
Well I had left it open to see if anyone could help advise on the final bit I wrote..

I'd also like to know how change this to hide the child categories when desired as an option, yet include those child category posts in both the parent category post count and search results.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The viewer will learn how to count occurrences of each item in an array.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now