Home>

I want to check the existence of an article from two pieces of information ($post_info1, $post_info2). Is WP_Query faster than the following get_posts?

// Check existence
$exist = get_posts (array (
    'post_type' =>'post',
    'meta_query' =>array (
       'relation' =>'AND',
       array (
         'key' =>'key1',
         'value' =>$post_info1,
         'type' =>'CHAR',
         'compare' =>'='
       ),
       array (
         'key' =>'key2',
         'value' =>$post_info2,
         'type' =>'CHAR',
         'compare' =>'='
       )
    ),
));


I only know that the information to be acquired is different, and that get_posts is via WP_Query, but I'm not sure how to use it, so I'd like to tell you about that.

Thanks for your understanding.

  • Answer # 1

      

    Is WP_Query faster than get_posts below?

    I don't think it will change.

      

    Acquired information is different

    Depends on the specification method (contents). I think that it does not change greatly with the degree that some default values ​​are different.

    【get_posts () | Function | WordPress Developer Resources】
    https://developer.wordpress.org/reference/functions/get_posts/

      

    Individual use

    I think it depends on people. I often use WP_Query, but the reporter of the following article seems to use get_posts.

    [So far! Exploring the characteristics of [WP_Query] and [get_posts ()] in WordPress | Web production company LIG in Tokyo Ueno]

Related articles