Beth Elwell
Beth Elwell

Reputation: 81

Adapting a wordpress search to search multiple fields

I am trying to adapt an existing search to allow for searches for not only staff names but also their titles (job roles).

Currently the following is the form method:

<form method="get" action="<?php echo get_post_type_archive_link('staff'); ?>" class="search" data-behavior="search">

and this is the get_post_type_archive_link function

function get_post_type_archive_link( $post_type ) {
    global $wp_rewrite;
    if ( ! $post_type_obj = get_post_type_object( $post_type ) )
        return false;

    if ( ! $post_type_obj->has_archive )
        return false;

    if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
        $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
        if ( $post_type_obj->rewrite['with_front'] )
            $struct = $wp_rewrite->front . $struct;
        else
            $struct = $wp_rewrite->root . $struct;
        $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
    } else {
        $link = home_url( '?post_type=' . $post_type );
    }

    return apply_filters( 'post_type_archive_link', $link, $post_type );
}

Upvotes: 0

Views: 497

Answers (2)

Quebrando Cabeca
Quebrando Cabeca

Reputation: 325

I do not know if I understand right, but follows a custom search for titles, content and also for Custom Fields (if you have)

  <?php //LOOP PARA BUSCA COM TODOS CUSTOM FIELD
  $keyword = get_search_query();
  $keyword = '%' . like_escape( $keyword ) . '%'; // Thanks Manny Fleurmond
 // SEARCH FROM ALL CUSTOM FIELDS
$post_ids_meta = $wpdb->get_col( $wpdb->prepare( "
ALTER TABLE wp_posts ADD INDEX (postmeta)
SELECT DISTINCT post_id FROM {$wpdb->postmeta}
WHERE meta_value LIKE '%s'
", $keyword ) );

// SEARCH FOR TITLE AND CONTENT
$post_ids_post = $wpdb->get_col( $wpdb->prepare( "
ALTER TABLE wp_post s ADD INDEX (posts);
SELECT DISTINCT ID FROM {$wpdb->posts}
WHERE post_title LIKE '%s'
OR post_content LIKE '%s'
", $keyword, $keyword ) );
$post_ids = array_merge( $post_ids_meta, $post_ids_post );

$wpq = array(
'taxonomy'=>'segmento',

'post_status' => 'publish',
'post_type' => 'CUSTOM-POST-TYPE',
'paged' => get_query_var('paged'),
'post__in' => $post_ids,
'order' => 'ASC',

  );

  query_posts($wpq);

Good Luck

Upvotes: 0

JakeParis
JakeParis

Reputation: 11210

This function has nothing to do with searching your Wordpress database. Instead, it is echoing out a url which tells the form where to submit. What you need to find out is what happens once the form is submitted to that url. There are some standard Wordpress functions and filters for searching, but I'm guessing based what you've shown here that there is also some custom functionality in the code somewhere which is executing a search somehow using the word "staff".

Where is this function? Is this in a plugin? A Theme? Are there other functions around it? I would suggest looking around in the general area of this function for something that has some sql queries and paste that function here for us to look at.

Upvotes: 2

Related Questions