Solved

Combine 2 functions in wordpress

Posted on 2014-09-15
6
228 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. 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 : Go t…

920 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

14 Experts available now in Live!

Get 1:1 Help Now