WP_Query Meta_key is text value and need to sort as numeric not working

A meta_value field is always text; the fieldtype in the database structure is longtext, so unless you’re explicitly typecasting when retrieving it (i.e. $price = (float)$custom[‘price’][0]; – which won’t help with ordering in the first place), you can’t ‘adjust’ the custom field data – it’s always stored as text. Having said that, you should simply … Read more

Best filter to use for modifying custom fields on a post?

Ok, this is actually about the output as you said in your comment. I suppose you know, you can do modification like this: // assumed a single result is returned as string $your_post_meta = get_post_meta( get_the_ID(), ‘your_post_meta’, true ); // lets add something $your_modified_post_meta=”My post meta: ” . $your_post_meta; So far so obvious I would … Read more

Add custom fields to existing posts (admin pages)

As Andreas Krischer suggested, I fixed using an script to update all the old values and then only using the wp_insert_post to create the custom field for the new posts. The script that I used: INSERT INTO wp_postmeta( post_id, meta_key, meta_value ) SELECT wp_posts.ID, ‘municipi’, ” FROM wp_posts WHERE wp_posts.post_status=”publish” AND wp_posts.post_type=”post” Here I’m updating … Read more

Get post ID from a link

I guess you’re not after the current post/page ID, but rather trying to find the ID for some url from your site. Then you should try out the handy url_to_postid() WordPress core function. It returns the post/page ID if it exists, else 0. Example: echo url_to_postid( ‘http://example.com/2014/11/14/hello-world’ ); // Outputs the post ID as 1 … Read more

Warn user that data may be lost for custom pages

you have to make a javascript code similar to this… jQuery(function ($) { name = $(‘#name’).val(); $(‘#name’).data(‘old_value’,name); window.onbeforeunload = function () { if ($(‘#name’).data(‘old_value’) !== $(‘#name’).val()) return ‘You have unsaved changes!’; } }); here’s a demo page… try closing the page after changing the value of the textbox there…

Toggle custom fields?

You could do this with jquery. I have a similar code on use which you could probably easily edit to your needs: load the admin js (I load css too but you can remove that) function wpse181868_admin_css_js() { wp_register_style( ‘bones_admin_css’, get_template_directory_uri() . ‘/library/css/admin.css’, false ); wp_enqueue_style( ‘bones_admin_css’ ); //adding scripts file in the footer wp_enqueue_script( … Read more