How To I Access Object Data In Nativescript Wp Post
Read Time: 5 mins Languages:
So far in this series you've learned how to admission WordPress meta data, and work with the arrays in which they are returned. Nosotros don't just add custom fields to WordPress posts so we tin display this data, only it as well so nosotros can sort by it.
Now that yous know how to retrieve and display meta information, information technology's fourth dimension to acquire how to customize the WordPress loop to return only posts with specific meta values.
Using WP_Query to Query by Meta Field Value
In gild to customize what posts WordPress returns based on meta fields, we demand to use WP_Query
and specify a meta_query
. If, for example, nosotros had a custom post type called 'films' that had a custom field called 'director' we could query for films whose manager was a director of one of the three Star Wars films.
Take a expect at the code beneath and see if you can use your mastery of arrays that you gained in the last office to understand what is going on with the meta_query
, which I will suspension down what is going on under the code.
$sw_args = array( 'post_type' => 'films', 'meta_query' => array( array( 'fundamental' => 'director', 'value' => array( 'George Lucas', 'Richard Marquand', 'Irvin Kershner' ), 'compare' => 'IN', ) ) ); $query = new WP_Query( $sw_args ); if ( $the_query->have_posts() ) { echo '<h2>Films By Star Wards Directors</h2>'; echo '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<li>' . get_the_title() . '</li>'; } repeat '</ul>'; } /* Restore original Post Information */ wp_reset_postdata();
As you tin can run into we take an array of director'due south names nestled inside of three other arrays. Permit's have information technology apart, piece past piece.
Kickoff, we start an array for our WP_Query
args. After our first argument 'post_type', we begin an array to house our meta_query
arguments.
Within of that nosotros specify which key to look for--in the instance 'director'. We likewise provide an array of values to search for in that key.
The terminal argument is how to compare those values, in this case we specify "IN" to think any posts with these values in the key director.
Other Comparisons
What if nosotros wanted films that where directed past a director of a Star Wars film, but wan't to exclude the Star Wars Prequels? We can but add another array of arguments to our meta_query
, but this time for value use an array of those films titles for the key film_title
and for compare, use 'NOT Similar' to exclude post that accept these values in the film_title
field.
$sw_args = array( 'post_type' => 'films', 'meta_query' => array( assortment( 'key' => 'director', 'value' => array( 'George Lucas', 'Richard Marquand', 'Irvin Kershner' ), 'compare' => 'IN', ), array( 'central' => 'film_title', 'value' => 'Phantom Menace', 'Attack of the Clones', 'Revenge of the Sith' ), 'compare' => 'NOT LIKE' ), ) ); $query = new WP_Query( $sw_args );
Now WordPress will look for films by these three directors, whose title is not 1 of the 3 prequels.
Displaying Meta Fields in WP_Query
And then far I've shown you lot how to use WP_Query
to detect posts that have specific values for a custom field, merely non how to show those fields.
Showing these fields is pretty much the same as before, but instead of using get_the_ID()
to specify ID for get_post_meta()
we specify it in an object context. And so in our loop, which you can see below, the ID is retrieved a petty differently using $query->post->ID
.
$sw_args = array( 'post_type' => 'films', 'meta_query' => array( array( 'key' => 'managing director', 'value' => array( 'George Lucas', 'Richard Marquand', 'Irvin Kershner' ), 'compare' => 'IN', ), assortment( 'cardinal' => 'film_title', 'value' => 'Phantom Menace', 'Attack of the Clones', 'Revenge of the Sith' ), 'compare' => 'NOT Similar' ), ) ); $query = new WP_Query( $sw_args ); if ( $the_query->have_posts() ) { repeat '<h2>Films By Star Wards Directors</h2>'; echo '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<li><ul>'; echo '<li>' . get_the_title() . '</li>'; echo '<li>' . get_post_meta( $query->post->ID, 'director', truthful ). '</li>'; echo '</ul></li>'' } echo '</ul>'; } /* Restore original Mail Information */ wp_reset_postdata();
Using WP_User_Query
Simply like in the last part when we used WP_Query
to find posts with certain values for diverse custom fields, we can use WP_Query
'south user tabular array equivalent WP_User_Query
.
For example, if we had a custom field chosen subscriber_level
and wanted to notice only those users who had the subscriber level of extra_special
or super_special
nosotros could, much the same way nosotros searched for films directed past the iii directors of the Star Wars films:
$args = array( 'meta_query' => assortment( assortment( 'fundamental' => 'subscriber_level', 'value' => array('extra_special', 'super_special' ); 'compare' => '=' ) ) ); $user_query = new WP_User_Query( $args ); if ( ! empty( $user_query->results ) ) { echo '<h3>Actress and Super Special Users</h3>'; echo '<ul>'; foreach ( $user_query->results as $user ) { echo '<li>' . $user->display_name . '</li>'; } echo '</ul>'; }
Simply similar with WP_Query
, we tin combine different comparisons to further control which users our query returns. This next case combines the last query with a a query, this time of the wp_users
table itself for whatsoever users whose names are Luke, Han or Leia.
$args = array( 'meta_query' => array( array( 'cardinal' => 'subscriber_level', 'value' => assortment('extra_special', 'super_special' ); 'compare' => '=' ) ), 'search' => array( 'Luke', 'Han', 'Leia' ), 'search_columns' => array( 'user_nicename', 'display_name' ), ); $user_query = new WP_User_Query( $args ); if ( ! empty( $user_query->results ) ) { repeat '<h3>Actress and Super Special Users Named Luke, Han or Leia</h3>'; repeat '<ul>'; foreach ( $user_query->results as $user ) { echo '<li>' . $user->display_name . '</li>'; } echo '</ul>'; }
Wrapping Up
In this series yous've been introduced to WordPress meta data and learned some lessons near cadre PHP concepts forth the manner.
You've learned how to become the values from the fields that shop post and user meta data and how to construct queries for posts and users based on meta values. With this knowledge you lot tin are prepared to piece of work with a few custom fields and on your style to using WordPress as a complex content management organisation.
Did you find this mail useful?
How To I Access Object Data In Nativescript Wp Post,
Source: https://code.tutsplus.com/articles/mastering-wordpress-meta-data-querying-posts-and-users-by-meta-data--wp-34606
Posted by: gatewoodpooked.blogspot.com
0 Response to "How To I Access Object Data In Nativescript Wp Post"
Post a Comment