Wordpress custom field in post database - wordpress

I have added a field in wordpress posts table named "parent_location". How to get the value of this field in db front end? I have tried get_post_custom_values('parent_location', $post_id) function but it not working. Can any body help me out?
Thanks in advance, don't down vote

You added the field as a new column in wp_posts or added a new metadata entry to a single post? If its to a post then just do a get_post_meta(postID, 'parent_location', true); It its a new column into wp_posts, then should you not be able to just to a standard;
<?php
$my_id = 100;
$post = get_post($my_id);
$parent_loc = $post->parent_location;
?>
Not sure if that will work, I've not tried to get a custom column via that method before. If it doesn't, just roll your own SQL query; http://codex.wordpress.org/Class_Reference/wpdb

Related

how to insert checkout page custom field value wp_posts table in wp

I have added the custom field check out page. I want to add this custom field's value to the order list in the wp_posts table
Here I have added a Row delivery_option in wp_posts table
http://prntscr.com/rbdjcy
Thanks
in WP you dont need/shouldn't change wp tables are this will be overwritten in the next update/upgrade. To save those values use WP php functions (forget database as WP functions will handle saving/getting for you from meta table)
So to add or update use
update_post_meta($post_id, $meta_key, $meta_val);
here meta key will be "delivery_option" and meta_value will be anything you want to store. $post_id will be current post/product id , you can get as get_the_ID(); Also if you do not want this custom field show up in WP default custom fields metaboxes, you may prefix your meta key with "_" underscore so "delivery_option" will be become "_delivery_option"
Then to get this data back :
$delivery_val = get_post_meta($post_id, 'delivery_option', true);
or
$delivery_val = get_post_meta($post_id, '_delivery_option', true); //_used to keep it hidden from WP default custom fields metaboxes
Source:
https://developer.wordpress.org/reference/functions/get_post_meta/
https://developer.wordpress.org/reference/functions/update_post_meta/
https://developer.wordpress.org/reference/functions/add_post_meta/
PS. Even there is add_post_meta , normally we use update_post_meta as it will add if no value exists or will update if any old value exists.

Can't get post ID and category using wp_insert_post_data

I would like to create post titles automatically. The post title is made from the category and category ID before the post is created in the database but does not get the category and ID.
Any help will be much appreciated, Thank You.
function my_post_titles($data, $postarr){
$postid = $postarr['ID'];
$postcategory = $postarr['post_category'];
$data['post_title'] = $postcategory."-".$postid;
return $data;
}
add_filter('wp_insert_post_data', 'my_post_titles', '99', 2 );
Couple of things to be aware of here: wp_insert_post_data is the filter used by wp_insert_post to allow changes to its first parameter, so to understand your problem, you should check how wp_insert_post works.
https://developer.wordpress.org/reference/functions/wp_insert_post/
Essentially, if the post you are creating a new post, then it should not have any ID (as it's not even created and saved to the database yet). To have access to the post ID, I suggest you use save_post hook rather than wp_insert_post. save_post is the action triggered after the post is created or updated. For example:
add_action('save_post', function($post_id) {
$title = get_the_title($post_id);
if ($title) {
return; // if the post has already has a title, then do nothing
}
// Otherwise, update the post title here
});
Also, $postarr['post_category'] is an array, not a string, so to get the correct information, you must convert it to string before concat it with the post_id.

Add meta field just after post is published

I'm trying to add a custom field of event_month when a post is published or saved. I'm using the save_post action and getting the contents of a custom field containing the date and trying to store this in a separate custom field with just the month. This works perfectly when saving a post that has already been created. I've shown my code below.
add_action('save_post', 'update_event_date');
function update_event_date($post_id){
$post_type = get_post_type($post_id);
$event_datee = get_post_meta($post_id, '_EventStartDate', true);
if ($post_type == 'tribe_events'){
$month = date("m",strtotime($event_datee));
update_post_meta($post_id, 'event_month', $month);
}
}
The problem arises when creating a new post. I think this is because the action fires before the _EventStartDate meta has been created and therefore the month can't be taken from this.
The hook is firing correctly and as intended when saving/updating a post but doesn't correctly get the month from the meta when creating a new post.
I'd really appreciate it if someone could provide me with some guidance.
To access post meta passed together with yours, you can do something like this:
$event_datee = get_post_meta($post_id, '_EventStartDate', true);
foreach($_POST['meta'] as $meta){
if($meta['key'] == '_EventStartDate'){
$event_datee = $meta['value'];
}
};

get all pages page by custom field

I am try to get all my page in wordpress that have a custom field like this.
$query = new WP_Query(array('meta_key=showHome&meta_value=true&post_type=page'));
print_r(count($query));
But this return only one element!
any idea to how get all pages with meta_key=showHome
remove meta_value=true from query string
$query = new WP_Query(array('meta_key=showHome&post_type=page'));
print_r(count($query));

Add Meta Post function not working

I am using add post meta function to save some data and its not working
<?php
//include '../../../wp-blog-header.php';
$unique = "true";
$pageID = $_GET['postID'];
echo "pageID:";
echo $pageID;
echo "</br>";
$num_posts = $_GET['num_posts'];
echo "num_posts: ";
echo $num_posts;
echo "</br>";
$num_posts_meta_key = "num_posts";
add_post_meta($pageID, $num_posts_meta_key, $num_posts , $unique) or update_post_meta($pageID, "num_posts" , $num_posts);
?>
Can someone help me out?
In first page I am getting all values from textboxes or checkboxes in javascript and then i am passing it in URL to next page where add_post_meta function is there.
I tried using method POST ...but then it doesnt work for me. It just submit the page and come back w/o doing anything on 1st page. I tried with GET method..but nothing works.
Hence I decided to take all values like num of post, post id in javascript and then from there pass it with url by using window.location.
I am very new to wordpress plugin coding. I thought POST method in my plugin is conflicting with some other post method in post.php..not sure though..
I am writing plugin for admin panel.
not sure what your problem is.. are you sure you're passing the right postID parameter? does the post exist in the database?
You don't really need to do add_post_meta() or update_post_meta.
From the manual:
The first thing this function will do
is make sure that $meta_key already
exists on $post_id. If it does not,
add_post_meta($post_id, $meta_key,
$meta_value) is called instead and its
result is returned.
<?php
// This minimum code should work, though you should really check that a post
// with this id does exist.
update_post_meta($_GET['postID'], "num_posts" , $_GET['num_posts']);
?>

Resources