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

Create custom field on post draft or publish?

Add an array and choose one for the value. add_action(‘draft_post’, ‘add_custom_field’); function add_custom_field($post_ID) { global $wpdb; if(!wp_is_post_revision($post_ID)) { $colors = array(‘white’, ‘green’, ‘blue’); add_post_meta($post_ID, ‘random_color’, $colors[array_rand($colors, 1)], true); } }

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

How should I use posts_where to change meta_value from a string to integer?

Try this: add_filter(‘posts_where’, ‘unquote_numeric_meta_value’, 10, 2); function unquote_numeric_meta_value($where, $args) { $value = isset($args->query_vars[‘meta_value’]) ? $args->query_vars[‘meta_value’] : false; if(is_numeric($value)) $where = str_replace(“‘{$value}'”, $value, $where); return $where; }

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

do_shortcode based on _thumbnail_id

// function to show a banner using query of banner post type function show_banner( $att ) { // Set default $att values if none is provided // $att is an array so access the shortcode value via $att[‘thumbnail_id’] // in the [banner thumbnail_id=”12″] shortcode $atts = shortcode_atts( array( ‘thumbnail_id’ => ”, ), $atts, ‘banner’ ); … Read more

Categories shortcode Tags database, post-meta, shortcode

delete value 0 in post meta [closed]

This question is off-topic because your is a simple PHP error, not related to WordPress. You are trying to check if a variable is empty using: if ( $meta != $empty ) { but this is not the right way, in fact this code compare the target variable $meta with another variable $empty that is … Read more

Categories post-meta Tags post-meta

Group posts by meta_key

This is the solution, according to the asker. He used the get() method of the WP_Query class to retrieve an array of values (colors), each stored with the meta key color. Because [color] => ‘Yellow’ may appear in the array more than once, he then filtered the array through the PHP function, array_unique() which will … Read more

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

Custom query with query_posts doesn’t show post without certain meta_key

Well, you can’t sort by field that isn’t there. You can modify query conditionally only in those categories which have that field and let default query work in rest. It would be something like: if( is_category(‘abs’) ) query_posts( array_merge( array(‘orderby’ => ‘meta_value’, ‘meta_key’ => ‘field_order’, ‘order’ => ‘Desc’ ), $wp_query->query ) );

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

get_post_meta – get a single value

If every meta value has the key f_name $meta = get_post_meta($post->ID, “company_wp_box_g”, true); echo $meta[‘fname’]; If you’re not sure of the contents & want to search for the first one available $name = false; $meta = get_post_meta($post->ID, “company_wp_box_g”, false); foreach($meta as $array) { if(isset($array[‘f_name’])) { $name = $array[‘f_name’]; break; } } echo $name;

Categories post-meta Tags post-meta

post meta data clearing on autosave

Using save post action I check state: <?php add_action(‘save_post’ ,’my_save_postdata’); function my_save_postdata ( $post_id ) { if ( defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE ) { return $post_id; } // logic }

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

Use a function to update post meta based on other post meta

Here are two simple ways to do this: One is to use the the_content-filter to update the post “hotness”, or you can schedule an event that will run daily and update the “hotness” once a day. First approach: the_content-filter Turn you code in to a “callable” function: function update_hotness( $post_id, $echo = false ) { … Read more

Categories functions Tags functions, post-meta

How to Update post status using meta data in Custom post TYpe

If I’m understanding correctly, you want to use your own select form to update a post’s published status instead of the publish/unpublish buttons? Something like this would work for that: function save_my_data($post_id){ if($_POST[‘publish’] == ‘yes’ && get_post_status($post_id) != ‘publish’){ //whatever your post variable is remove_action(‘save_post’, ‘save_my_data’); //if you don’t unhook the function you’ll have an … Read more

Categories custom-post-types Tags custom-post-types, post-meta
Older posts
Newer posts
← Previous Page1 … Page179 Page180 Page181 … Page203 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
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 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