user2593041
user2593041

Reputation: 397

How do I make query use exact post title

I am creating a custom search query in wordpress and the post title isn't matching. I want it to say the posttitle and the fullname have to be exact matches, or nothing should come up under the results. My Post_titles are all 5 digits numbers. But as of right now, I can type in the correct fullname but use any numbers that don't match the post title and it still shows up. Both need to be exact matches. Any suggestions on what to do here?

 <?php 
           /* Search Results */ 
 $patientid = '48392'; //Example only but must match exactly AND fullname should match exact too
 $fullname = 'John Doe'; //Example only. Exact match IS working on this but not AND

 $args = array('posts_per_page' => 2, 'post_type' => 'patients', 'post_title' => $patientid,
 'meta_query' => array(
  array(
    'key'     => 'fullname',
    'value'   => $fullname,
    'compare' => '=',
     ),
 ),);

  query_posts($args);
 ?>

Upvotes: 0

Views: 1867

Answers (1)

Shibi
Shibi

Reputation: 1383

Use get_page_by_title, which allows you to query by Title, ID or Post Object. query_posts doesn't have the post title as a parameter and is deprecated (Use WP_Query instead if you want to do a custom query).

$patient = get_page_by_title($patientid, 'OBJECT', 'patients'); // get the post object by title and cpt

if($patient) {
    $patient_fullname = get_post_meta($patient->ID, 'fullname', true); // get the post meta
    if($patient_fullname == $fullname) {
        // here both conditions are true.
    }
}

Upvotes: 2

Related Questions