最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Single field to search for matches on post ID or meta values

programmeradmin3浏览0评论

I'm sure someone else had this need somewhere... I just couldn't find it!

So I have a search field where I want to write something and what I write returns me WooCommerce orders.

This is my working code that searchs for first and last billing names:

$args = array(
    'post_type'       => 'shop_order',
    'posts_per_page'  => '-1',
    'post_status'     => array_keys( wc_get_order_statuses() ),
    'meta_query'      => array(
        'relation'    => 'OR',
        array(
            'key'     => '_billing_first_name',
            'compare' => 'LIKE',
            'value'   => $_GET['sc'],
        ),
        array(
            'key'     => '_billing_last_name',
            'compare' => 'LIKE',
            'value'   => $_GET['sc'],
        )
    )
);

But I also want to be able to search for post ID so that I can allow, on that one field, for users to search for order ID, first and last billing names.

How can I achieve this?

EDIT:

After the suggestion from the user mukto90, I did some changes and the code now works perfectly.

$args = array(
    'post_type'       => 'shop_order',
    'posts_per_page'  => '-1',
    'post_status'     => array_keys( wc_get_order_statuses() ),
);

if( 'shop_order' == get_post_type( $sc_esc ) ) {
    $args['p'] = $sc_esc;
} else {
    $args['meta_query'] = array(
        'relation'    => 'OR',
        array(
            'key'     => '_billing_first_name',
            'compare' => 'LIKE',
            'value'   => $sc_esc,
        ),
        array(
            'key'     => '_billing_last_name',
            'compare' => 'LIKE',
            'value'   => $sc_esc,
        )
    );
}

I'm sure someone else had this need somewhere... I just couldn't find it!

So I have a search field where I want to write something and what I write returns me WooCommerce orders.

This is my working code that searchs for first and last billing names:

$args = array(
    'post_type'       => 'shop_order',
    'posts_per_page'  => '-1',
    'post_status'     => array_keys( wc_get_order_statuses() ),
    'meta_query'      => array(
        'relation'    => 'OR',
        array(
            'key'     => '_billing_first_name',
            'compare' => 'LIKE',
            'value'   => $_GET['sc'],
        ),
        array(
            'key'     => '_billing_last_name',
            'compare' => 'LIKE',
            'value'   => $_GET['sc'],
        )
    )
);

But I also want to be able to search for post ID so that I can allow, on that one field, for users to search for order ID, first and last billing names.

How can I achieve this?

EDIT:

After the suggestion from the user mukto90, I did some changes and the code now works perfectly.

$args = array(
    'post_type'       => 'shop_order',
    'posts_per_page'  => '-1',
    'post_status'     => array_keys( wc_get_order_statuses() ),
);

if( 'shop_order' == get_post_type( $sc_esc ) ) {
    $args['p'] = $sc_esc;
} else {
    $args['meta_query'] = array(
        'relation'    => 'OR',
        array(
            'key'     => '_billing_first_name',
            'compare' => 'LIKE',
            'value'   => $sc_esc,
        ),
        array(
            'key'     => '_billing_last_name',
            'compare' => 'LIKE',
            'value'   => $sc_esc,
        )
    );
}
Share Improve this question edited Mar 10, 2022 at 19:58 Pedro asked Mar 9, 2022 at 2:42 PedroPedro 338 bronze badges 1
  • 1 What if a user searches with 123, and you have an order with ID = 123 also another order with _billing_last_name = 123? (I know that's very unlikely, but you need to be sure) – mukto90 Commented Mar 9, 2022 at 2:52
Add a comment  | 

1 Answer 1

Reset to default 1

Use this code instead-

$args = array(
    'post_type'       => 'shop_order',
    'posts_per_page'  => '-1',
    'post_status'     => array_keys( wc_get_order_statuses() ),
    'meta_query'      => array(
        'relation'    => 'OR',
        array(
            'key'     => '_billing_first_name',
            'compare' => 'LIKE',
            'value'   => $_GET['sc'], // you should escape this
        ),
        array(
            'key'     => '_billing_last_name',
            'compare' => 'LIKE',
            'value'   => $_GET['sc'], // you should escape this
        )
    )
);

// if there is an order that has the same ID as what user inputs, include that in the query too
if( 'shop_order' == get_post_type( $_GET['sc'] ) ) {
    $args['p'] = $_GET['sc']; // you should escape this
}
发布评论

评论列表(0)

  1. 暂无评论