A place to keep snippets of useful WordPress SQL.
Do a select based on some meta field of a post ( i.e. search for a post that has a particular metafield )
1 2 3 4 5 6 7 8 | SELECT p.ID,p.post_title, MAX(CASE WHEN pm1.meta_key = 'id_vim_node' then pm1.meta_value ELSE NULL END) as field_i_want FROM wp_posts p LEFT JOIN wp_postmeta pm1 ON ( pm1.post_id = p.ID) where pm1.meta_key = 'id_vim_node' GROUP BY p.ID, p.post_title |
Output WP_Query sql code (very useful for debugging WP_Query )
if in the loop
1 | <?php echo $GLOBALS['wp_query']->request; ?> |
outside of loop
1 2 | $customPosts = new WP_Query($yourArgs); echo "Last SQL-Query: {$customPosts->request}"; |
based on this post on stackexchange
Recent Comments