jfar_2020
jfar_2020

Reputation: 161

Search custom taxonomy titles and not the posts themselves

I have a custom post type called 'rejectedpayments' and I have a custom taxonomy called 'rejpayments-category' which is assigned to this CPT. I then have an archive template that displays all the taxonomy term titles only (NOT the actual posts) then I can click the taxonomy term title to see all posts that are in that taxonomy. There is a search field on this page and this is what I'm struggling with. The search just searches by custom post type so it searches every post in the 'rejectedpayments' CPT but I want it to only search the actual term titles.

So if I have:

Term 1

Term 2

Term 3

Currently if I search for 'Post' there are obviously 6 posts with this in the title so they come up (it doesn't search the term title at all) so the titles, Term 1, Term 2 and Term 3 are what I want to search only.

The only filter on the form is obviously the CPT name but I wouldn't have a clue how to add an additional filter so it also adds a parameter of search term name only.

<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
    <div class="row form-wrap">
        <div class="col-md-10 padding-none">
            <input type="text" name="s" placeholder="Search all rejected payments"/>
            <input type="hidden" name="post_type" value="rejectedpayments" />
        </div>
        <div class="col-md-2 padding-none">
            <input type="submit" alt="Search" value="Search" />
        </div>
    </div>
</form>

My current archive page that lists all Term names only within the specific custom post type

<?php $wcatTerms = get_terms('rejpayments-category', array('hide_empty' => 0, 'parent' =>0)); 
  foreach($wcatTerms as $wcatTerm) : 
?>
<div class="results-table">
  <div class="row info-tables">
    <div class="col-md-6">   
      <a href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>"><?php echo $wcatTerm->name; ?></a>
      <?php
        $wsubargs = array(
        'hierarchical' => 1,
        'show_option_none' => '',
        'hide_empty' => 0,
        'parent' => $wcatTerm->term_id,
        'taxonomy' => 'rejpayments-category'
         );
           $wsubcats = get_categories($wsubargs);
           foreach ($wsubcats as $wsc):
          ?>
          <p><a href="<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>"><?php echo $wsc->name;?></a></p>
          <?php
            endforeach;
          ?>  
        </div>
        <div class="col-md-6"> 
           LINK
        </div>
     </div>
  </div>
  <?php 
     endforeach; 
  ?>

When using @kofeigen' answer, I get the following error:

Notice: Function wpdb::prepare was called incorrectly. The query argument of wpdb::prepare() must have a placeholder. Please see Debugging in WordPress for more information. (This message was added in version 3.9.0.) in /var/www/vhosts/domainnamehere/wp-includes/functions.php on line 5865 Parse error: syntax error, unexpected '...' (T_ELLIPSIS) in /var/www/vhosts/domainnamehere/wp-content/themes/themename/cat-rejected-payments.php on line 55

Upvotes: 0

Views: 898

Answers (1)

kofeigen
kofeigen

Reputation: 1635

The following updates to the archive page could be used to generate the desired search results.

Submit the form to the archive page

The archive page could submit the form to itself. The form action attribute could be changed as follows:

<?php

    ...

    global $wp;
    $current_url = home_url( add_query_arg( array(), $wp->request ) );?>

    ...

    <form role="search" action="$current_url" method="get" id="searchform">

Add a name attribute to the submit button

<input name="submittedForm" type="submit" alt="Search" value="Search" />

Get an unfiltered terms list (default) or a terms list filtered by the search form

/**
 * Untested code.
 * You may wish to change the ORDER BY clause.
 * 
 * @param string $searchKey Search key submitted from the search form.
 * 
 * @return object[] $wcatTerms Array of taxonomy term objects.
 */
function get_filtered_taxonomy_terms( $searchKey ) {
    global $wpdb;
    $custom_taxonomy = 'CUSTOM_TAXONOMY_NAME';

    $table_prefix = $wpdb->prefix;

    $escaped_search_key = $wpdb->esc_like( $searchKey );

    $sql  = "";
    $sql .= "SELECT T.term_id, T.slug, T.name, TT.term_taxonomy_id, TT.description, TT.parent, TT.count";
    $sql .= " FROM {$table_prefix}terms T";
    $sql .= " JOIN {$table_prefix}term_taxonomy TT ON ( TT.term_id = T.term_id )";
    $sql .= " WHERE TT.taxonomy = '${custom_taxonomy}'";
    $sql .= " AND T.name LIKE '%" . $escaped_search_key . "%'";
    $sql .= " ORDER BY T.name ASC";
    $sql .= ";";

    $wcatTerms = $wpdb->get_results( $sql, 'OBJECT' );

    return $wcatTerms;
}


if ( isset( $_GET ) && isset( $_GET['submittedForm'] ) ) {
    // Search form submitted.
    // Find array of all terms (parents and children)
    // with term name partially matching the search key.
    $wcatTerms = get_filtered_taxonomy_terms( $_GET['s'] );

    ...

} else {
    // Default terms list.
    $wcatTerms = get_terms('rejpayments-category', array('hide_empty' => 0, 'parent' =>0));

    ...

}

Render results

With hierarchical terms, different approaches can be used to render the list depending on maximum depth, etc. The WordPress Walker class could be extended and used to render the list. An example of such an extension is the WordPress Walker_Nav_Menu class.

Upvotes: 0

Related Questions