Solved

Combine 2 functions in wordpress

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

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…
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 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