Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.
103 votes

How do I retrieve the slug of the current page?

As per other answers, slug is stored in the post_name property. While it could be accessed directly, I prefer the (underused) get_post_field() function for accessing post properties which have no ...
user avatar
  • 97.7k
74 votes

Get posts by meta value

What you are asking for is a meta_query $args = array( 'meta_key' => 'custom-meta-key', 'meta_query' => array( array( 'key' => 'cp_annonceur', 'value' => ...
user avatar
  • 63.4k
46 votes
Accepted

Explanation of update_post_(meta/term)_cache

Object cache everywhere WordPress tries to reduce the number of database queries as much as possible. For example, anytime you get a meta field or a taxonomy field, before querying the database, ...
user avatar
  • 44.6k
35 votes

How do I retrieve the slug of the current page?

EDIT 5 APRIL 2016 After digging for more reliability, I ended up doing this answer to the following post which leads to this edit: (Be sure to check it out) $GLOBALS['wp_the_query'] vs ...
user avatar
33 votes

How to check if post meta key exists or not in wordpress database

In case anyone else stumbles across this old question like I just did, it looks like the best way to handle this is using metadata_exists() see https://developer.wordpress.org/reference/functions/...
user avatar
  • 451
31 votes
Accepted

Custom post meta field effect on the performance on the post

To answer this, I have gone and done some tests on this, and the results was actually mind blowing. Here is my test To this yourself, set yourself up with a test page. Just simply copy page.php, ...
user avatar
26 votes

How can I get the post ID from a WP_Query loop?

get_the_ID() can (only) be used within the loop. This retrieves the ID of the current post handled by the loop. You can use it on it's own if you need it only once: $dish_meta = get_post_meta( ...
user avatar
  • 2,109
25 votes

How to only display posts whose meta_value field is not empty?

This is an old question, but it seems Wordpress has fixed this "missing feature": now, according to Wordpress Codex is possible to check for existence (or non-existence) of the meta key, like this '...
user avatar
25 votes
Accepted

Can wp_query return posts meta in a single request?

By default, WP_Query returns the standard WP_Post objects for the posts being queried. I believe with some clever rewrite and use of the filters given in WP_Query you can add objects to the returned ...
user avatar
20 votes

How do I retrieve the slug of the current page?

The simple way to get the slug is with: <?php echo basename(get_permalink()); ?>
user avatar
  • 317
20 votes
Accepted

Difference between meta keys with _ and without _

The underscore prefix are private, these meta fields will be hidden and will not be shown as custom fields in the post backend screens. Those meta fields without the underscore prefix are public ...
user avatar
19 votes

Query all posts where a meta key does not exist

Using a custom query, this worked for me: SELECT * FROM wp_posts as posts WHERE posts.post_type = 'post' AND NOT EXISTS ( SELECT * FROM `wp_postmeta` ...
user avatar
  • 301
19 votes

How to get custom post meta using REST API

WP API has a rest_prepare_post filter (or rest_prepare_CPT if you are working with custom posts) which you can use to modify the JSON response. In your case it will be rest_prepare_joblisting. ...
user avatar
19 votes
Accepted

How to update_post_meta value as array

You do this: $item->content_multiple = get_post_meta($item->ID, '_menu_item_content_multiple', true); Note how you set the third parameter of get_post_meta() to true, which means that only one ...
user avatar
  • 19.7k
16 votes

Get post with multiple meta keys and value

This should do it. The default relation is AND so that won't need to be specified. $args = array( 'post_type' => 'wpse_cpt', 'meta_query' => array( array( 'key' ...
user avatar
15 votes

How to hook a filter to catch get_post_meta when alternate a custom field output?

After a lot of messing around with this, I think I found a fairly good solution here. I realize this is over a year after you asked but this was bothering me and I couldn't find a good solution until ...
user avatar
14 votes

Get posts by meta value

There are two ways to do that: Intercept the main query on pre_get_posts: add_action( 'pre_get_posts', function( $query ) { // only handle the main query if ( ! $query->is_main_query() ) ...
user avatar
  • 49.8k
14 votes
Accepted

Why WordPress choose data serialization over json_encode?

I think, not 100% sure that this was the real reason the WP developers took this approach, but common sense tells me that serialize preserves the variable types and have a mini built in error ...
user avatar
  • 351
13 votes

What is the different between an attachment in wp_posts and an attachment in wp_postmeta?

An attachment is a Post The posts table contains information on the attachment post. In WordPress every uploaded media has it's own post entry, where post_type is 'attachment' and post_status is '...
user avatar
  • 44.6k
12 votes

Adding meta tag without plugin

Although I would advice you to avoid this and use a plugin for this work. Because only a dedicated plugin can help SEO. "WordPress SEO" from yoost is a great plugin for SEO. That being said, here is ...
user avatar
  • 8,161
12 votes
Accepted

Orderby meta_value only returns posts that have existing meta_key

As stated in @ambroseya's answer, its supposed to work like that. Once you declare a meta query, even if you aren't looking for a specific value, it will only query posts with that meta key declared. ...
user avatar
11 votes

How to make search include data from wp_postmeta?

Add this to your plugin or functions.php file of your theme. The example below will include 'your_key' in search. You can include all your keys by repeating the array. function me_search_query( $...
user avatar
  • 119
11 votes

Remove Wordpress.org Meta link

There's a super easy way to remove the wordpress.org link from the Meta widget. Just use: add_filter( 'widget_meta_poweredby', '__return_empty_string' ); and it's gone!
user avatar
  • 63.9k
11 votes

Check if Post Title exists, Insert post if doesn't, Add Incremental # to Meta if does

A more updated method may use the post_exists() function like so: if( isset( $_POST['submit'] ) ){ $post_title = sanitize_title( $_POST['post_title'] ); $new_post = array( 'post_title' ...
user avatar
11 votes

Get posts by meta value

I used custom select (might be better performance) $posts = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = 'cp_annonceur' AND meta_value = 'professionnel' LIMIT 1", ARRAY_A)...
user avatar
11 votes
Accepted

Making Wordpress author follow button, need missing ingredient

I would advise you not to use the user meta. Although your problem is caused by: update_post_meta( $author_id, "_user_followed", $followed_USERS ); // Add user ID to author meta update_post_meta( $...
user avatar
  • 51.9k
11 votes

How do I query for posts by partial meta key?

It is quite difficult to concretely answer your question. The first part is easy though. I recently did something similar on stackoverflow Meta keys are are compared and match exactly. WP_Query have ...
user avatar
11 votes
Accepted

What's the point of get_post_meta's $single param?

In general, storing PHP serialized data in database in a bad idea. It can be easily understood if you use multple key-value pairs of data in one field row, that is, you use an array or a object with ...
user avatar
  • 19.7k
10 votes
Accepted

Getting attachments by meta value

I've spent hours of research and found the solution a few minutes after writing this question: $args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', <<-- ...
user avatar
  • 321
10 votes
Accepted

Use REGEXP in WP_Query meta_query key

Here's one experimental idea: Assume we got: post A with the custom field location1 as UK - London post B with the custom field location2 as France - Paris post C with the custom field ...
user avatar
  • 63.9k

Only top scored, non community-wiki answers of a minimum length are eligible