Get post ID or product ID by Meta value

1) Method One

global $wpdb;
$results = $wpdb->get_results( "select post_id, meta_key from $wpdb->postmeta where meta_value = 'this is my example value.'", ARRAY_A );

2) Method Two

$args = array(
	'post_type'		=>	'page',
	'meta_query'	=>	array(
		array(
			'value'	=>	'this is my example value.'
		)
	)
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
  while( $my_query->have_posts() ) {
    $my_query->the_post();
    // Do your work...
  } // end while
} // end if
wp_reset_postdata();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s