Home > Software engineering >  How to modify wp_query in wordpress
How to modify wp_query in wordpress

Time:04-15

i am working with wordpress,And i want to pass one parameter name "post_title" in wp_query,So i can change query and can get correct data,But my query is not changed(not displaying as i expected),I tried with following code

$args = array(
    'post_type' => 'user',
    'posts_per_page' => -1,
     'post_title' => $_GET['post_title'],
    'post_status' => [ 'publish']
);
$query = new WP_Query($args);

when i print my $query->request then i getting following query

[request] = "SELECT   MZAGsQVeposts.* FROM MZAGsQVeposts  WHERE 1=1  AND MZAGsQVeposts.post_type = 'user' AND ((MZAGsQVeposts.post_status = 'publish')) ORDER BY MZAGsQVeposts.post_date DESC";

But expected query is

"SELECT   MZAGsQVeposts.* FROM MZAGsQVeposts  WHERE 1=1  AND MZAGsQVeposts.post_type = 'user' AND ((MZAGsQVeposts.post_status = 'publish')) AND MZAGsQVeposts.post_title='Lisa I' ORDER BY MZAGsQVeposts.post_date DESC";

Where i am wrong ? Thanks in advance.

CodePudding user response:

Unfortunately you can't query posts by title in wp_query, it's not a recognised argument. (See WP_Query for more info) however you can use get_page_by_title and pass the title that way and it'll return the post object with that title.

  • Related