You can use WP_Query
for this:
$query = new WP_Query( array( 'post_type' => 'my_cpt' ) );
This will query only posts of the type my_cpt.
To search only for posts with a certain first or last name, you have to extend the query. For this you need to add the field meta_query
:
$query = new WP_Query( array(
'post_type' => 'my_cpt' ),
'meta_query' => array(
'relation' => 'OR', //defaults to AND
array(
'meta_key' => 'first_name',
'meta_value' => $search_string,
'compare' => 'LIKE'
),
array(
'meta_key' => 'last_name',
'meta_value' => $search_string,
'compare' => 'LIKE'
)
));
To use this query you probably gonna need a custom search form where you would change the search parameter from s
to something like search_my_cpt
.
After you did this you need to edit your search.php // this is where the results are displayed
:
you need to add something like this:
if( isset($_GET['search_my_cpt'])) {
//your new query
//display your search results
}
I didnt test this code, but this should bring u very close to what u want to achieve