Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

post-meta

Order by meta value or date?

If you can explain it in SQL, you can query for it! There are three places where we want to change the default query: SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type=”post” AND (wp_posts.post_status=”publish”) AND wp_postmeta.meta_key = ‘startDate’ AND CAST(wp_postmeta.meta_value AS CHAR) < ‘2011-03-23’ GROUP BY wp_posts.ID ORDER … Read more

Categories query-posts Tags date-time, post-meta, query-posts

What is the index [0] for on post meta fields?

Each post meta key can have multiple values. So you get the values by default as an array; the first entry (0) is the oldest. get_post_custom() fetches all meta keys with their values. You could simplify your code with: $price = get_post_meta( $post->ID, ‘price’, TRUE ); $comake = get_post_meta( $post->ID, ‘coMake’, TRUE ); When the … Read more

Categories custom-field Tags custom-field, metabox, post-meta

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. If you want to include all posts sort them by the meta key, use the following code: $args = … Read more

Categories custom-field Tags custom-field, post-meta, sort

Correctly delete posts with meta and attachments [duplicate]

@s_ha_dum suggests that Post meta will be deleted automatically. Therefore because his reputation suggests he knows what he is talking about, this solution only handles Post attachments. I’d suggest checking out the docs for the before_delete_post() hook, as it’s quite handy to be able to check out what Post Type is being deleted, etc. add_action(‘before_delete_post’, … Read more

Categories custom-post-types Tags attachments, custom-post-types, customization, post-meta

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’ => ‘post_code’, ‘value’ => ‘432C’, ), array( ‘key’ => ‘location’, ‘value’ => ‘XYZ’, ), ), ); $query = new WP_Query( $args );

Categories custom-post-types Tags custom-post-types, post-meta, wp-query

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 location3 as USA – New York Then we could use, for example: $args = [ ‘meta_query’ => [ ‘relation’ => … Read more

Categories wp-query Tags filters, meta-query, mysql, post-meta, wp-query

Difference between meta keys with _ and without _ [duplicate]

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 fields and shows up as custom fields in the post screens

Categories custom-field Tags custom-field, post-meta

How do I query for posts by partial meta key?

Unfortunately you cannot perform a meta_query using a LIKE comparison on the meta_key value when using WP_Query. I’ve been down this road… Instead you have a couple other options if you want to maintain like status relationships as post meta and not user meta and or meta in a custom table. Option 1 requires no … Read more

Categories wp-query Tags post-meta, wp-query

How to get custom post meta using REST API

$post in the callback function is an array, not an object. So you cannot use $post->id. Change it to $post[‘id’] and it should work: function slug_get_phone_number($post, $field_name, $request) { return get_post_meta($post[‘id’], ‘_phone’, true); } I recommend to change _phone to phone_number or something else without underscore prefix. Because _ is often used with private meta … Read more

Categories custom-field Tags custom-field, post-meta, rest-api

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

This would need a query. So building on your code: <?php $postTitle = $_POST[‘post_title’]; $submit = $_POST[‘submit’]; if(isset($submit)){ global $user_ID, $wpdb; $query = $wpdb->prepare( ‘SELECT ID FROM ‘ . $wpdb->posts . ‘ WHERE post_title = %s AND post_type = \’stuff\”, $postTitle ); $wpdb->query( $query ); if ( $wpdb->num_rows ) { $post_id = $wpdb->get_var( $query ); … Read more

Categories post-meta Tags post-meta, wp-insert-post
Older posts
Newer posts
← Previous Page1 … Page200 Page201 Page202 … Page204 Next →
+ More

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 2026 Read For Learn
Next Page »
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress