• Hi All,

    I have been searching high and low for code to make a custom search page where visitors can make a selection more complex than just clicking on category or keyword links. Long story short, very hard to find and no replies to my questions posted for over a year so here is some code I wrote for all you looking to do similar things. I haven’t written the search part yet, but that should be pretty straightforward. I am not the best PHP coder, so this code is a bit clunky, but it works. If anyone can come up with a nicer cleaner version, please post it!

    Thanks!
    Satre

    FOR KEYWORDS
    This code will retrieve your keywords with checkboxes set for the term_id that can be used in your search:

    <?php
    
    $args=array(
    'orderby' => id,
    'order' => 'ASC',
    'taxonomy' => 'post_tag'
    ); 
    
    $keys=get_categories($args); 
    
    foreach($keys as $keyword) {
    
        echo "<input type='checkbox' name='check' value='$keyword->term_id'>";
    	echo "$keyword->cat_name [$keyword->count]";
    
    }
    ?>

    FOR CATEGORIES LISTED AS PARENTS WITH CHILDREN
    This code below will list your categories with parents in bold and children with checkboxes. If you want parents to have checkboxes too, just uncomment the relevent code on line 28. You can also add the checkbox code to the highest parents if you want. They are printed on line 13. There is a counter, $i, that you will have to customize to your own data. It is used to put line breaks in the appropriate locations.

    <?php
    $i = 0;
    $categories = get_categories();
    foreach ($categories as $cat) {
    	$i = $i + 1; // counts iterations, used for formatting below
        if($cat->parent < 1) {
        	$cat_name = $cat->cat_name;
        	$catid = get_cat_ID($cat_name);
       		$termorder = $cat->term_order;
    			if ($i = 13 OR $i = 14) { //value depends on your number of records so you don't print returns before 1st printed record
    			echo "<br>";
    			}
       		echo "<strong>$cat_name</strong>"; //remove if you don't want the highest heierarchy
    
        	$args=array(
          	'orderby' => name,
          	'order' => 'ASC',
          	'child_of' => $catid
          	);
        	$catys=get_categories($args);
          	foreach($catys as $category) {
    
    					$catparent = $category->parent;
    					if ($catid == $catparent) {
    					 	if ($i > 12) { //value depends on your number of records so you don't print returns before 1st printed record
    						echo "<br>";
    						}
    				//	    echo "<input type='checkbox' name='check' value='$category->term_id'>"; (uncomment if you want to select parents)
    						echo "<strong>$category->cat_name</strong>";
    
    				}
    				else{
            		echo "<input type='checkbox' name='check' value='$category->term_id'>";
    				echo "$category->cat_name [$category->count]";
    				}
    
            }  
    
        } 
    
    }
    ?>

Viewing 1 replies (of 1 total)
  • Thread Starter satre

    (@satre)

    If someone can enlighten me on how to get the children to list in order of term_order (from ‘wp_terms’ table, I’m not sure which tables the ‘get_categories’ function uses), it would be greatly appreciated. You can see I try to do it with this on line 9:
    $termorder = $cat->term_order;
    and it kind of works, but the ordering is erratic and not in the actual ‘term_order’ order.

Viewing 1 replies (of 1 total)
  • The topic ‘categories and keywords with checkboxes for search’ is closed to new replies.