banner



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.

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.

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.

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:

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.

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel