0

I have about 25,000 posts here (and rising). All of them under a Custom Post Type "lead". Each post has meta information, including a variable called "uniqid". this uniqid is a url parameter. now i need the post id where exactly this uniqid exists.

Now my question is if there is a way to speed up this determination.

I have tried 2 ways once a wp_query and get_results.

I ask because this function that determines the post id is always noted by plugin "query monitor" that it is slow.

These are the 2 approaches, both work. I just wonder if it is possible to speed up here?

The WP_Query solution.

$post_id = false;
$args = array(
    'posts_per_page'    =>  -1,
    'post_type'         =>  'lead',
    'fields' => 'ids',
    'orderby' => 'date',
    'order'   => 'ASC',
    'post_status'       =>  'publish',
    'meta_key'          => 'uniqid',
    'meta_value'        => $uniqid
);

$query = new WP_Query( $args );


if( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();

        // Get Post ID for return
        $post_id = get_the_id();

        // Break loop, when matching id was found       
        $uniqidGPM = get_post_meta( $post_id, 'uniqid' , true );

        if($uniqidGPM == $uniqid) {
            $post_found = true;
            break;
        }

    }
}

wp_reset_postdata();
return $post_id;

The select get_results solution.

$post_id = false;
global $wpdb;

$selectQuery = "SELECT wp_wkdm_posts.ID FROM wp_wkdm_posts  INNER JOIN wp_wkdm_postmeta ON ( wp_wkdm_posts.ID = wp_wkdm_postmeta.post_id ) WHERE 1=1  AND ( ( wp_wkdm_postmeta.meta_key = 'uniqid' AND wp_wkdm_postmeta.meta_value = '".$uniqid."' )) AND wp_wkdm_posts.post_type = 'lead' AND ((wp_wkdm_posts.post_status = 'publish')) GROUP BY wp_wkdm_posts.ID ORDER BY wp_wkdm_posts.post_date ASC";
$selectQueryResult = $wpdb->get_results($selectQuery);

if (empty($selectQueryResult)) {
    return $post_id;
}else{
    $post_id = $selectQueryResult[0]->ID;
    return $post_id;
};
enky
  • 40
  • 8
  • What is the point of the check inside the loop with your first attempt? You already have the limitation to only posts that have this unique id set inside the query itself. – CBroe Jul 14 '21 at 09:18
  • Also, what is the point of ordering by date (or by _anything_, really) - do you have more than one post with such a “unique” ID? – CBroe Jul 14 '21 at 09:19
  • @CBroe Yeah your right. the while loop in the first attempt is not necessary. The Sorting. There I must explain briefly. user generate here content and this is processed by us. That's why I sort the posts from new to old, because my thought was that then I'm more likely to be at the posts I need. If I need post ID 20000, I don't have to start looking at post ID 1, but rather at post ID 25000. So once I have to go through only 5000 posts, the other way around 20000 posts. – enky Jul 14 '21 at 09:27
  • 1
    Evaluation of the WHERE clause happens first, only after the relevant records have been determined, _those_ do then get sorted. So unless you can have _multiple_ records with the unique ID you are looking for, and want to pick only the newest of those, this wins you nothing. – CBroe Jul 14 '21 at 09:29
  • 1
    From my experience i can tell you that as soon as postmeta gets fat, you are going down on a very bad road if you want to look after performance. You should do all you can to avoid postmeta grow big. If that is too late just try to clean it as much as you can – Diego Jul 14 '21 at 09:33
  • @CBroe So if I understand you correctly, the only result, still sorted, which brings absolutely nothing. Or are all posts sorted first and then filtered. (Sorry, English is not my native language) – enky Jul 14 '21 at 09:54
  • 1
    No, the posts are filtered first, and then sorted. https://stackoverflow.com/a/24128128/1427878 – CBroe Jul 14 '21 at 10:14

1 Answers1

1

Please use this meta_query condition on the same query, it helps you.

$args = array(
    'post_type' => 'lead',
    'posts_per_page'    =>  -1,
    'post_status'       =>  'publish',
    'meta_query' => array(
        'key' => 'uniqid',
        'value' => 'YOUR_VALUE'
    )
);

$query = new WP_Query($args);
Vishal P Gothi
  • 987
  • 5
  • 15