search - Query based on title, with 'compare' => 'IN'

admin2025-01-08  7

I wanna do a query based on title with a predefiend variable. I cant seem to make it work. What am I missing?

Making the query without the 'compare' isnt a problem.

    $args = array('post_type' => 'udbyder', 
                  'posts_per_page' => -1, 
                  'order' => 'ASC',
                  'meta_query' => array(
                  'relation' => 'AND',
                    array(
                    'key' => 'title',
                    'value' => $test,
                    'compare' => 'IN'
    ) 
));

I wanna do a query based on title with a predefiend variable. I cant seem to make it work. What am I missing?

Making the query without the 'compare' isnt a problem.

    $args = array('post_type' => 'udbyder', 
                  'posts_per_page' => -1, 
                  'order' => 'ASC',
                  'meta_query' => array(
                  'relation' => 'AND',
                    array(
                    'key' => 'title',
                    'value' => $test,
                    'compare' => 'IN'
    ) 
));
Share Improve this question asked Jul 10, 2018 at 8:27 MathiasMathias 111 bronze badge 4
  • Why should it work? Title is not a meta, IN works in different way (it works for arrays, not strings)... It looks like you wrote some random code and expect WP to work like you want it to... What do you want to achieve exactly? – Krzysiek Dróżdż Commented Jul 10, 2018 at 8:47
  • I was hoping for it to work ;-) Ive done it with customfields, but a title. I want the query to fetch the titles that has $test (a POST value form an input field) 'IN' it. – Mathias Commented Jul 10, 2018 at 11:20
  • It sounds like you also misunderstand what IN is for. Can you please describe what you want to do in more detail. What is $test? What types of values will it have and what types of posts should it return? – Jacob Peattie Commented Jul 10, 2018 at 12:11
  • Probably :-( I wanted to make a query based an input field. My query should be something like this: Only take posts with post_type 'udbyder' that has $test (whatever was written in the search/input field) in its title. For instance, if $test equals BUS - I want results, such as, "A big bus", "The blue bus" and "Bus". Not only "Bus", as it would be if I just typed 'title' => $test Thank you. – Mathias Commented Jul 11, 2018 at 7:46
Add a comment  | 

1 Answer 1

Reset to default 0

This just isn't possible with WP_Query on its own. The post title isn't meta and there's no built-in capacity to query based on title in this way.

The way you can do it is to filter the SQL directly to add your logic. You can even do this in such a way that you can add a custom argument to WP_Query, which I'll call 'titles', which will let you pass an array of post titles. To support this argument add this filter to posts_where:

function wpse_308130_posts_where( $where, $query ) {
    global $wpdb;

    // Check if our custom argument has been set on current query.
    if ( $query->get( 'titles' ) ) {
        $titles = $query->get( 'titles' );

        // Escape passed titles and add quotes.
        $titles = array_map( function( $title ) {
            return "'" . esc_sql( $title ) . "'";
        }, $titles );

        // Implode into string to use in IN() clause.
        $titles = implode( ',', $titles );

        // Add WHERE clause to SQL query.
        $where .= " AND $wpdb->posts.post_title IN ($titles)";
    }

    return $where;
}
add_filter( 'posts_where', 'wpse_308130_posts_where', 10, 2 );

That will let you pass a list of post titles you want to retrieve posts for like this:

$posts = new WP_Query( array(
    'post_type'      => 'udbyder', 
    'posts_per_page' => -1, 
    'order'          => 'ASC',
    'titles'         => ['Title one', 'Title two'],
) );

That will return posts with the titles "Title one" or "Title two".

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267990a1238.html

最新回复(0)