Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

meta query - Wordpress meta_query slow loading

So I have 2 custom fields that I'm querying. The first, 'feature' is a true/false field and video_id is a text input field (which is hidden until another field that has a condition to show it). I basically want to get all posts where feature is false and video_id doesn't exist. I have a solution that works but it takes around 30seconds to execute which is too long:

             $newsArgs = array(
                'post_type'      => 'post',
                'posts_per_page' => $newsLen,
                'post__in' => $stickies,
                'tax_query' => array(
                    'relation' => 'AND',
                    $user->getTaxQuery('all')
                ),
                'meta_query' =>  array(
                    'relation' => 'AND',
                    array(
                        'key' => 'video_id',
                        'compare' => 'NOT EXISTS'
                    ),
                    array(
                        'relation' => 'OR',
                        array(
                            'key' => 'feature',
                            'compare' => 'NOT EXISTS'
                        ),
                        array(
                            'key' => 'feature',
                            'value' => '1',
                            'compare' => '!='
                        )
                    )
                )
            );

Looking at the wordpress documentation I tried this based on what I understood from it, however this did not work for me:

             $newsArgs = array(
                'post_type'      => 'post',
                'posts_per_page' => $newsLen,
                'post__in' => $stickies,
                'tax_query' => array(
                    'relation' => 'AND',
                    $user->getTaxQuery('all')
                ),
                'meta_query' =>  array(
                    'relation' => 'AND',
                    array(
                        'key' => 'video_id',
                        'compare' => 'NOT EXISTS'
                    ),
                    array(
                        'key' => 'feature',
                        'value' => '1',
                        'compare' => '!='
                    )
                )
            );

Also if I just try querying all posts with no video_id but including feature and the other way round, all posts with video_id but no feature. The query works and is instant. Is there something I'm doing wrong here, is there anyway I can help improve the performance? Any help would be much appriciated. I'm also tryng to avoid writing a mySQL query as the $user->getTaxQuery I have under tax_query would be a ball ache. Thanks in advance

question from:https://stackoverflow.com/questions/65829894/wordpress-meta-query-slow-loading

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...