Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: WP_Query search criteria on 'post_name'

I'm using WP_Query (pretty standard). It all works great.

However, I have a particular modification to make, where, if the user enters the specific post name in the URL, the search will return only the post that matches that post_name value.

See my code below with a comment about the particular line not working.

<?php

$getPeople = array(
    'post_type' => 'person',
    'posts_per_page' => -1,
    // I want this below to only return me the post with this specific value.
    // This doesn't error, but doesn't work either.
    // I know it seems counter-productive to a 'search' but this particular case requires it.
    // This has a hard-coded value at the moment.
    'post_name' => 'rebecca-atkinson',
    'orderby' => 'meta_value',
    'meta_key' => 'last-name',
    'order' => 'ASC',

    'meta_query' => array(
        array(
            'key' => 'gender',
            'value' => $theGender,
        )
    ),

    'tax_query' => array(

        'relation' => 'OR',

        array(
            'taxonomy' => 'accent',
            'field' => 'slug',
            'terms' => $theAccent,
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'style',
            'field' => 'slug',
            'terms' => $theStyle,
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'age',
            'field' => 'slug',
            'terms' => $theAge,
            'operator' => 'IN',
        ),

    )
);

$myposts = new WP_Query($getPeople);

?>

Your help would be greatly appreciated. If I could just see how to search on this specific 'slug' then that would be great.

Many thanks, Michael.

like image 472
Michael Giovanni Pumo Avatar asked Sep 29 '11 16:09

Michael Giovanni Pumo


People also ask

What is the difference between WP_Query and Get_posts?

The difference between wp_query() and get_posts() is basically the type of loop you want to use to display the posts on the front end. So in the end it mainly boils down to your preference and comfort.

How do I change the search query in WordPress?

You can simply add following code in your functions. php file in your WordPress theme directory. function searchfilter($query) { if ($query->is_search && ! is_admin() ) { $query->set('post_type',array('trip')); } return $query; } add_filter('pre_get_posts','searchfilter');

What is Post __ Not_in?

Avoid post__not_inIt's usually used to exclude certain post IDs from a query's results.


2 Answers

Instead of

'post_name' => 'rebecca-atkinson',

use:

'name' => 'rebecca-atkinson',
like image 62
sxalexander Avatar answered Sep 20 '22 07:09

sxalexander


In addition to my answer in the comments above, I thought I'd post it as an official answer too:

I have to use 'name' and NOT 'post_name'.

For example:

'name' => 'rebecca-atkinson' 

Hope this helps someone in future.

like image 20
Michael Giovanni Pumo Avatar answered Sep 20 '22 07:09

Michael Giovanni Pumo