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 ...
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' => ...
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, ...
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 ...
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/...
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, ...
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( ...
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
'...
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 ...
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()); ?>
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 ...
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`
...
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.
...
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 ...
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' ...
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 ...
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() )
...
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 ...
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 '...
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 ...
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. ...
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( $...
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!
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' ...
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)...
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( $...
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 ...
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 ...
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', <<-- ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
post-meta × 1939custom-field × 416
custom-post-types × 327
metabox × 221
wp-query × 169
posts × 149
php × 117
meta-query × 112
functions × 70
plugins × 66
plugin-development × 63
array × 61
query × 58
database × 57
loop × 56
save-post × 55
custom-taxonomy × 53
mysql × 53
query-posts × 52
customization × 46
woocommerce-offtopic × 44
meta-value × 44
wpdb × 41
attachments × 39
wp-insert-post × 38