Wp update post not updating - php

I'm trying to update a post title using wp_update_post()
First I get the ID of the post:
global $post;
$post_to_edit = get_post($_GET['post_id']);
Then the form to update which it actually gets the title from the post to edit:
<form action="" id="primaryPostForm" method="POST">
<label for="postTitle">Title of your post</label>
<input type="text" name="postTitle" id="postTitle" class="form-control" value="<?php echo esc_html( $post_to_edit->post_title ); ?> ">
<input type="hidden" name="postId" id="postId" value="<?php echo $_GET['post_id']; ?>">
<input type="submit" name="submit_post" value="Update">
</form>
Then below on the same page I have:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['submit_post'])) {
$data = array(
'ID' => $_POST['postId'],
'post_title' => $_POST('postTitle')
);
wp_update_post( $data );
}
}
?>
But it's not updating the post title.

Your code is wrong,
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['submit_post'])) {
$data = array(
'ID' => $_POST['postId'],
'post_title' => $_POST['postTitle'] //you used wrong brackets
);
wp_update_post( $data );
}
}
?>

Related

How to pass multiple checkbox values to Wordpress Search? custom_tax[]=val1&custom_tax[]=val2

I am trying to build a custom search form in Wordpress where the user would be able to select custom taxonomies to filter the posts against.
I am unfortunately getting this warning:
Warning: urlencode() expects parameter 1 to be string, array given in /wordpress/wp-includes/formatting.php on line 5343, which is this function:
function wp_basename( $path, $suffix = '' ) {
return urldecode( basename( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ), $suffix ) );
}
Here is my code of the form:
<form action="/" method="get">
<?php $post_type = !empty( $_GET['post_type'] ) ? $_GET['post_type'] : 'company'; ?>
<label for="search" class="block"><?php echo __('Search', 'mydomain'); ?>:</label>
<input type="radio" name="post_type" value="company" <?php echo $post_type == 'company' ? 'checked' : ''; ?>>Companies</input>
<input type="radio" name="post_type" value="auditor" <?php echo $post_type == 'auditor' ? 'checked' : ''; ?>>Auditors</input>
<input type="text" name="s" id="search" value="<?php the_search_query(); ?>" class="block" placeholder="<?php echo __('Type here'); ?>" />
<div>
<?php $certificates = get_terms([
'taxonomy' => 'certificate',
'hide_empty' => false
]);
foreach($certificates as $certificate) :
?>
<input type="checkbox" name="certificate[]" id="certificate-<?php echo $certificate->slug; ?>" value="<?php echo $certificate->slug; ?>" <?php echo (!empty($_GET['certificate']) && in_array($certificate->slug, $_GET['certificate']) ) ? 'checked' : ''; ?>> <?php echo $certificate->name; ?>
<?php
endforeach;
?>
</div>
<input type="hidden" value="<?php echo pll_current_language(); ?>" name="lang" id="lang" />
<input type="submit" value="<?php echo __('Search'); ?>" />
</form>
and this is how I attempt to see the search results:
http://wordpress.local/?post_type=auditor&s=&certificate%5B%5D=iso-22000&certificate[]=iso-9001
Any ideas how to bypass this warning and still submit the checkboxes as an array? (custom_tax[])

Wordpress: How to add publish/draft button in Admin Post Lists

I try to add publish/draft button in Wordpress Admin Post Lists but it doesn’t work well in a certain situation.
Does Not Work
New post -> Save as Draft -> Publish using new button in Adomin Post Lists
Does Work
New post -> Publish -> Draft -> Publish using new button in Adomin Post Lists
Thank You all in advance for help or any other ways :D
Reference :
https://wordpress.stackexchange.com/questions/17794/publish-pending-article-from-front-end-with-a-button
(this doesnt work)
Here’s functions.php code
function show_publish_button($post_id){
$pub_form = <<<EOF
<form name="make_it_publish" method="post" action="">
<input type="hidden" name="pid" id="pid" value="{$post_id}">
<input type="hidden" name="mi_publish" id="mi_publish" value="mi_publish">
<input type="submit" name="submit" id="submit" value="publish">
</form>
EOF;
echo $pub_form;
}
function show_draft_button($post_id){
$pri_form = <<<EOF
<form name="make_it_draft" method="post" action="">
<input type="hidden" name="pid" id="pid" value="{$post_id}">
<input type="hidden" name="mi_draft" id="mi_draft" value="mi_draft">
<input type="submit" name="submit" id="submit" value="draft">
</form>
EOF;
echo $pri_form;
}
function change_post_status($post_id,$status){
$time = current_time('mysql');
wp_update_post(
array(
'ID' => $post_id,
'post_status' => $status,
'post_name' => $post_id,
'post_date_gmt' => get_gmt_from_date( $time )
)
);
}
add_action( 'admin_init', function() {
if (isset($_POST['mi_publish']) && $_POST['mi_publish'] == 'mi_publish'){
if (isset($_POST['pid']) && !empty($_POST['pid'])){
change_post_status((int)$_POST['pid'],'publish');
}
}
if (isset($_POST['mi_draft']) && $_POST['mi_draft'] == 'mi_draft'){
if (isset($_POST['pid']) && !empty($_POST['pid'])){
change_post_status((int)$_POST['pid'],'draft');
}
}
});
function manage_posts_columns($columns) {
$columns['publish'] = "publish";
$columns['draft'] = "draft";
return $columns;
}
function add_column($column_name, $post_id) {
if($column_name == 'publish'){
$pub = "publish";
}
if($column_name == 'draft'){
$pri = "draft";
}
if ( isset($pub) && $pub ){
show_publish_button($post_id);
} else if ( isset($pri) && $pri ){
show_draft_button($post_id);
} else {
echo __('None');
}
}
add_filter( 'manage_posts_columns', 'manage_posts_columns' );
add_action( 'manage_posts_custom_column', 'add_column', 10, 2 );
The reason is nest form tag.
Admin Post Lists is huge form and I added form tag. That's why it didn't work.
Simple way is using ajax and wp_update_post.

Editing and updating an item in CodeIgniter

I'm having trouble to edit and update an item in my first CodeIgniter project. When I click on the edit button, first of all, it loads the view with the inputs filled with the informations of that item on the database, but it doesn't load the bootstrap that I'm using.
Then, when I click the "Edit" button, returns lots of errors saying that:
my function on the controllers is missing an argument and the variable is undefined, and
my view has an undefined offset and is trying to get property of non-object.
All the other commands for the CRUD are working, except this.
The version of CodeIgniter I'm using is 2.2.6
Codes:
Model:
function edit($id)
{
$this->db->where('id', $id);
$query = $this->db->get('products');
return $query->result();
}
function update($data)
{
$this->db->where('id', $data['id']);
$this->db->set($data);
return $this->db->update('products');
}
Controller:
function edit($id)
{
$data['title'] = "Edit Product";
$data['product_data'] = $this->model->edit($id);
$this->load->view('productEdit', $data);
}
function update()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<span>', '</span>');
$validations = array(
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required|max_length[255]'
),
array(
'field' => 'price',
'label' => 'Price',
'rules' => 'trim|required|max_length[255]'
),
array(
'field' => 'stock_quantity',
'label' => 'In Stock',
'rules' => 'trim|required|max_length[255]'
)
);
$this->form_validation->set_rules($validations);
if ($this->form_validation->run() === FALSE) {
$this->edit($this->input->post('id'));
} else {
$data['id'] = $this->input->post('id');
$data['name'] = $this->input->post('name');
$data['price'] = $this->input->post('price');
$data['stock_quantity'] = $this->input->post('stock_quantity');
if ($this->model->update($data)) {
redirect('products');
} else {
log_message('error', 'Error');
}
}
}
View:
<?php echo form_open('products/edit/', 'id="form-products"'); ?>
<input type="hidden" name="id" value="<?php echo $product_data[0]->id; ?>"/>
<label for="nome">Product Name:</label><br/>
<input type="text" name="name" value="<?php echo $product_data[0]->name; ?>"/>
<div class="error"><?php echo form_error('name'); ?></div>
<label for="email">Price:</label><br/>
<input type="text" name="price" value="<?php echo $product_data[0]->price; ?>"/>
<div class="error"><?php echo form_error('price'); ?></div>
<label for="email">In Stock:</label><br/>
<input type="text" name="stock_quantity" value="<?php echo $product_data[0]->stock_quantity; ?>"/>
<div class="error"><?php echo form_error('stock_quantity'); ?></div>
<input type="submit" name="update" value="Update" />
<?php echo form_close(); ?>
Thank you for your time.
Try this one
Update your model
function edit($id)
{
$this->db->where('id', $id);
$query = $this->db->get('products');
return $query->row(0);
}
and then in your view.
<input type="hidden" name="id" value="<?php echo $product_data->id; ?>"/>
also change your form action
products/update/

wordpress update multiple posts post meta

I am trying to update multiple posts post meta at the same time. I have the following query:
<form action="" method="post">
<?php
$variations = new WP_Query();
$variations->query(array('showposts' => -1, 'post_type' => 'product_variation' )); while ($variations->have_posts()) : $variations->the_post(); ?>
<input name="regular_price[]" type="text" value="<?php echo get_post_meta(get_the_id(), "_regular_price", true); ?>" />
<input name="sale_price[]" type="text" value="<?php echo get_post_meta(get_the_id(), "_sale_price", true); ?>" />
<input name="item_id[]" type="hidden" value="<?php echo get_the_id(); ?>" />
<?php endwhile; wp_reset_query();?>
<input name="save" type="submit" />
I then have the following php to process the data:
<?php
if (isset($_POST['save'])) {
$ids = $_POST['item_id'];
$sales = $_POST['sale_price'];
foreach ($ids as $id){
update_post_meta($id,'_sale_price',$sale));
}
} ?>
For some reason the above does not save correctly. It will only save the last value, and apply this to all post meta. Is there something i am doing wrong?
I believe you need to add the id to $sale in you update_post_meta field. Like so:
<?php
if (isset($_POST['save'])) {
$ids = $_POST['item_id'];
$sales = $_POST['sale_price'];
foreach ($ids as $id){
update_post_meta($id,'_sale_price',$sale[$id]));
}
} ?>
You forgot the } for "for".
update .......;
}
}
danyo, I feel you have issue with $count. Please make sure that this variable have proper count value to update data in loop.

Array from form - Wordpress Metadata

I got a custom post type, with a form for storing some data (name, url) to display in a template.
What I want to know is how can I store those values in an array?
An example of my code:
<? function files_metadata(){
global $post;
$custom = get_post_custom($post->ID);
$name = $custom["name"][0];
$url = $custom["url"][0];
echo '<input type="hidden" name="files_metadata" id="files_metadata" value="' .wp_create_nonce('files_m'). '" />'; ?>
<label>Name: </label><br/>
<input id="name" name="name" value="<?php echo $name; ?>" />
<label>Url: </label><br/>
<input id="url" name="url" value="<?php echo $url; ?>" />
<? function save_meta_files($post_id) {
if (!wp_verify_nonce($_POST['files_metadata'], 'files_m')) return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
update_post_meta($post_id, "name", $_POST["url"]);
update_post_meta($post_id, "url", $_POST["url"]);
}
add_action('save_post', 'save_meta_files'); ?>
To this I want to add something like...
$url = $custom["url"][0];
$url2 = $custom["url"][1];
$url3 = $custom["url"][2];
<input id="url" name="url[0]" value="<?php echo $url; ?>" />
<input id="url2" name="url[1]" value="<?php echo $url2; ?>" />
<input id="url3" name="url[2]" value="<?php echo $url3; ?>" />
update_post_meta($post_id, "url", $_POST["url"][0]);
update_post_meta($post_id, "url2", $_POST["url"][1]);
update_post_meta($post_id, "url3", $_POST["url"][2]);
...but that actually works...
A passed array will be serialized into a string: http://codex.wordpress.org/Function_Reference/update_post_meta
update_post_meta(
$post_id,
'files_metadata',
array(
'name1' => $_POST['name1'],
'url1' => $_POST['url1']
'name2' => $_POST['name2'],
'url2' => $_POST['url2']
)
);

Categories