简体   繁体   中英

How to get search query like in get_results() to Wp_Query for my posts?

I try to transform get_results() to WP_Query(), but do not know, how.

get result:

    $myposts = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->posts
 WHERE post_type = 'advertisements'
 AND post_title != 'Automatický koncept'
 and post_title LIKE '%s'", '%'. $wpdb->esc_like( $text ) .'%')  );

WP_Query() :

 $args = array('post_type' => 'advertisements',
        'posts_per_page' => 2,
        );
$wp_query = new WP_Query( $args );

Is that possible? thanks for any advice

Good day, try this:

$args = array(
    'post_type'  => 'advertisements',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key'     => 'post_title',
            'value'   => 'Automatický koncept',
            'compare' => '!=',
        ),
        array(
            'key'     => 'post_title',
            'value'   => '%' . $wpdb->esc_like( $text ) . '%',
            'compare' => 'LIKE',
        ),
    ),
);

$wp_query = new WP_Query( $args );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM