Home › Forums › Add-ons › Repeater Field › Mistake in documentation ? : Query posts by custom fields › Reply To: Mistake in documentation ? : Query posts by custom fields
Finally i used wpdb to execute this request:
SELECT
wp_posts.ID
FROM
wp_posts
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
LEFT JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id AND mt1.meta_key = 'rc_cp_min_0')
LEFT JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id AND mt2.meta_key = 'rc_cp_max_0')
LEFT JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id AND mt3.meta_key = 'rc_cp_min_1')
LEFT JOIN wp_postmeta AS mt4 ON (wp_posts.ID = mt4.post_id AND mt4.meta_key = 'rc_cp_max_1')
LEFT JOIN wp_postmeta AS mt5 ON (wp_posts.ID = mt5.post_id AND mt5.meta_key = 'rc_cp_min_2')
LEFT JOIN wp_postmeta AS mt6 ON (wp_posts.ID = mt6.post_id AND mt6.meta_key = 'rc_cp_max_2')
WHERE
1 = 1
AND (wp_postmeta.meta_key = 'code pays'
AND wp_postmeta.meta_value = 'fr')
AND ((mt1.meta_value <= 7000 AND mt2.meta_value >= 7000)
OR (mt3.meta_value <= 7000 AND mt4.meta_value >= 7000)
OR (mt5.meta_value <= 7000 AND mt6.meta_value >= 7000))
AND wp_posts.post_type = 'distributeur'
AND wp_posts.post_status = 'publish'
LIMIT 0 , 1
Duration: 0.031 sec
It’s exactly the same code except that meta_key conditions are on join and not on where and i replaced INNER JOIN by LEFT JOIN.
My knowledge of Mysql optimization is limited so i don’t know exactly why it’s really faster but it works 🙂
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re hard at work on ACF 6.1, and Beta 1 is now available 🚀
— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.