Solved

Combine 2 functions in wordpress

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SharePoint and CAML query help 4 26
Code planning methods/tools? 5 55
How do I add date to text file name 15 28
Undefined variable with $_POST in PHP 5 38
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

732 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