Skip to content

Instantly share code, notes, and snippets.

@maheshwaghmare
Last active May 26, 2022 20:04
Show Gist options
  • Select an option

  • Save maheshwaghmare/99a9f07e1b6372fc6d31c57eefdc3234 to your computer and use it in GitHub Desktop.

Select an option

Save maheshwaghmare/99a9f07e1b6372fc6d31c57eefdc3234 to your computer and use it in GitHub Desktop.
Get all post IDs which have meta key `location` and meta value `Pune`.
<?php
/**
* Get Post By Meta
*
* Get all post IDs which have meta key `location` and meta value `Pune`
*
* @todo Change the `prefix_` and with your own unique prefix.
*
* @since 1.0.0
*/
if( ! function_exists( 'prefix_get_post_by_meta' ) ) :
function prefix_get_post_by_meta() {
global $wpdb;
// Get all IDs
$post_ids = $wpdb->get_results(
'SELECT `post_id` FROM `' . $wpdb->postmeta . '`
WHERE `meta_key` = \'location\'
AND `meta_value` = \'Pune\'
;'
);
return $post_ids;
}
// add_action( 'admin_head', 'prefix_get_post_by_meta' );
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment