Solved

Combine 2 functions in wordpress

Posted on 2014-09-15
6
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

726 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