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

How to get all custom fields of any post type

Add the following code in the functions.php for acf function get_all_meta($type){ global $wpdb; $result = $wpdb->get_results($wpdb->prepare( “SELECT post_id,meta_key,meta_value FROM wp_posts,wp_postmeta WHERE post_type = %s AND wp_posts.ID = wp_postmeta.post_id”, $type ), ARRAY_A); return $result; } function acf(){ $options = array(); $acf = get_all_meta(‘acf’); foreach($acf as $key => $value){ $options[‘post_type’][$value[‘post_id’]][‘post_id’] = $value[‘post_id’]; $test = substr($value[‘meta_key’], 0, 6); … Read more

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

Custom field values deleted when trashing custom post type

It seems that the action save_post gets fired when sending a post to the trash… Therefore, my custom metadata save function was being activated with no $_POST data to send thru it. To circumvent this, I wound up adding a nonce to the save function. function wpg_testimonial_author() { global $post; $custom = get_post_custom($post->ID); $testimonial_author_name = … Read more

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

Code to make a post sticky

The sticky posts are saved as an array of post IDs in the wp_options table. Hence, $stickies = get_option( ‘sticky_posts’ ); $stickies[] = $post_id; update_option( ‘sticky_posts’, $stickies ); will make the post in question sticky. EDIT: Even better, the core provides functions to stick and unstick posts (had to have ’em). stick_post( $post_id ); unstick_post( … Read more

Categories posts Tags post-meta, posts, sticky-post

WordPress database error: [Not unique table/alias: ‘wp_postmeta’]

You JOIN two elements both with the same table. When you need to join one table more than once, you need to make each join unique by giving the table alias. When WP makes the whole query, it takes that into account, but you have added your own code that doesn’t do that. So, first … Read more

Categories custom-taxonomy Tags custom-taxonomy, post-meta, search

How can I retrieve multiple get_post_meta values efficiently?

The way your code is setup is just wrong, you are making two database calls for each custom field and if you have between 2-30 fields like this then that means you make over 60 calls to the database which can be done with a single call using get_post_custom() for ex: $custom_fields = get_post_custom($post->ID); for … Read more

Categories posts Tags metabox, post-meta, posts

Lack of composite indexes for meta tables

A standard WordPress schema “sync” via dbDelta() will only add indexes, not drop them. Same goes for fields. We never touch the storage schema either, so it’d be the default for MySQL (which in latest versions is now InnoDB). On the face, a comment_id_meta_key index makes perfect sense. But when you look at how WordPress … Read more

Categories post-meta Tags comment-meta, mysql, post-meta

update_post_meta and update_field ony working when saving the post

The problem is that the function update_field from ACF works a little bit different from update_post_meta. Can you see it? update_post_meta( $post_id, $meta_key, $meta_value, $prev_value ); update_field( $field_key, $value, $post_id ); If you use update_post_meta you should in my opinion use get_post_meta to get the meta. If you use update_field you should use the_field or … Read more

Categories advanced-custom-fields Tags advanced-custom-fields, post-meta, save-post

Execute action after post is saved with all related post_meta records (data)

For NEW post type ‘post’ use draft_to_publish action hook: function fpw_post_info( $post ) { if ( ‘post’ == $post->post_type ) { // echo ‘<pre>’; print_r( $post ); echo ‘<br />’; // $meta = get_post_meta( $post->ID ); print_r( $meta ); echo ‘</pre>’; die(); // your custom code goes here… } } add_action( ‘draft_to_publish’, ‘fpw_post_info’, 10, 1 … Read more

Categories post-meta Tags post-meta, save-post

Storing revisions of metadata for custom post type

There’s a private filter you can (but shouldn’t) access: Defined in wp-includes/revision.php: /** * Filter the list of fields saved in post revisions. * * Included by default: ‘post_title’, ‘post_content’ and ‘post_excerpt’. * * Disallowed fields: ‘ID’, ‘post_name’, ‘post_parent’, ‘post_date’, * ‘post_date_gmt’, ‘post_status’, ‘post_type’, ‘comment_count’, * and ‘post_author’. * * @since 2.6.0 * @since 4.5.0 … Read more

Categories custom-post-types Tags custom-post-types, post-meta, revisions

Using get_post_meta with new_to_publish

That’s because the fields weren’t set then. Note: The »Autosave« process/request also doesn’t save them. Use the values from $_POST instead for your “new_to_publish” action. EDIT: Do NOT forget to escape and properly sanitize input data! Else you will open a security hole.

Categories custom-field Tags custom-field, post-meta, post-status
Older posts
Newer posts
← Previous Page1 … Page197 Page198 Page199 … 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