How to get WordPress Pages using Post Meta?

child-theme-in-wordpress

How to get WordPress Pages using Post Meta?

In some case we need to get the post id on the basis of the post-meta-name and its value to get this. Nonetheless, there have been projects where I’ve needed to retrieve the post ID from the Lets Have a real Example:
Before we search the post, we need the type of the the page_type and the Post metakey Name and Post Metakey Value. If we need the Post List using single contdition than, this can be done using two ways 1st one is using the WP_Query and 2nd one is get_post() and the third option is the sql

WordPress Pages using Post Meta Using WP_Query and The Loop

The first way that you can retrieve a post’s ID through it’s meta value is by using WP_Query. The only information you really need to know is the meta value for which you’re looking, although it also helps to have the post type handy.

To do this, you can setup the following query:

$args = array(
'post_type' => 'page',
'meta_query' => array(
array(
'value' => 'this is my example value.'
)
)
);
$my_query = new WP_Query( $args );

//From here, you can setup a standard variation of The Loop:

if( $my_query->have_posts() ) {
while( $my_query->have_posts() ) {
$my_query->the_post();
// your code here...
} // end while
} // end if
wp_reset_postdata();

But if you’re expecting to retrieve the single post, then there’s no need to use the while loop.

WordPress Pages using Post Meta Using the get_posts()

$args = array(
'posts_per_page' => -1,
'post_type' => 'page',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'post-meta-key-name-1',
'value' => 'post-meta-key-value-1',
'compare' => "=",
),

),
);
$post_lists= get_posts($args);

In many case we need the post details using the multiple Post Meta Values in that case we need to define the relation (which could be AND, OR) and the two different array to get that data here is the code for getting data in above condition.

$args = array(
'posts_per_page' => -1,
'post_type' => 'page',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'post-meta-key-name-1',
'value' => 'post-meta-key-value-1',
'compare' => "=",
),
array(
'key' => 'post-meta-key-name-2',
'value' => 'post-meta-key-value-2',
'compare' => "=",
),
),
);
$post_lists= get_posts($args);

even we can use the nesting in this case. We can use the sql’s AND +( OR ) query type for 3 Post Metabox values, here is the code for the post from the nested meta values

$args = array(
'posts_per_page' => -1,
'post_type' => 'page',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_my_custom_key',
'value' => 'Value I am looking for',
'compare' => '='
),
array(
'relation' => 'AND',
array(
'key' => '_my_custom_key_2',
'value' => 'Value I am looking for 2',
'compare' => '='
),
array(
'key' => '_my_custom_key_3',
'value' => 'Value I am looking for 3',
'compare' => '='
)
),
),
);
$post_lists= get_posts($args);

WordPress Pages using Post Meta using the SQL

it’s possible to use raw SQL to retrieve the results. To do this, we’ll need the name of the table in which the meta data is kept. In our case, it’s the wp_postmeta table (table prefix can be vary as per you setup) and the post meta value.
From here, you can use $wpdb and raw SQL for retrieving the results:

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

This is done, I personally don’t actually recommend doing this unless you’re having good knowledge of

SQL and have a deep understanding of query performance, the database schema etc.

Which is Better?

I personally try to use the WordPress API – in our case, WP_Query and the get_posts.This is the best way to get WordPress Pages from Post Meta.
For more detail you can refer on WordPress Codex.

About Pashupatinath Mishra

Pashupatinath Mishra is Software Developer and our Fulltime blogger. He is having good knowledge on the Different Technologies and also having shareable knowledge on Nutrition, Science Topics, Travel and History.

Website

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.