Adding post meta by URL in wp-admin - php

I have a script that order Wordpress posts by custom field and I would like to have a link that add's a custom field by clicking on it.
Like this (or so): <a href="/wp-admin/post.php?action=add_post_meta&my_meta_key&my_meta_value&post=1500&_wpnonce=...
My script uses:
$path = preg_replace('/wp-content.*$/','',__DIR__);
include($path.'wp-load.php');
global $wpdb;
Is this possble?

You don't need to look for any meta in post. You can simply do this on your single.php:
if( current_user_can('manage_options')) {
add_post_meta($post->ID, 'new_meta', 1, true);
}

You can also add this on functions.php
add_action('insert_post_meta', 'adding_new_custom_field');
function adding_new_custom_field ($post_id)
{
if ( $_POST['post_type'] == 'post' ) {
add_post_meta($post_id, 'new_meta_key_name', 'new meta value', true);
}
return true;
}

Why don't you script something like this:
if( current_user_can('manage_options')) {
// Check and get any post meta
$post_meta = get_post_meta( $post->ID, 'post_meta', true );
if (!empty($post_meta)){
add_post_meta($post->ID, 'new_meta', 1, true);
}
}
Now you can place this on your single.php, and... et voilĂ !
When you open the post, the new meta will be added.

Related

How to make Wordpress save_post_($post-type) hook works with multiple Custom Post Types

I have an issue related save_post_($post-type) hook and ACF gallery field.
I got this code snippet from somewhere. Let me explain: I created an ACF gallery field (acf-gallery is the field name) shown in Custom Post Type (cpt1 is the slug) then use this snippet to set the first image of this gallery as the featured image when saving just like what Woocommerce do.
But what if I want it to work with another Custom Post Type (let's say the slug is cpt2)? Can I use array( 'cpt1', 'cpt2' ) to replace cpt1? Is there a way to include multiple custom post types?
/* Set the first image generated by ACF gallery field as featured image */
add_action( 'save_post_cpt1', 'set_featured_image_from_gallery' );
function set_featured_image_from_gallery() {
global $post;
$post_id = $post->ID;
$images = get_field('acf_gallery', $post_id, false);
$image_id = $images[0];
if ( $image_id ) {
set_post_thumbnail( $post_id, $image_id );
}
}
I edited this snippet using save-post hook according to comments below. But I don't know if it's valid. Can someone help?
/* Set the first image generated by ACF gallery field as featured image */
add_action( 'save_post', 'set_featured_image_from_gallery' );
function set_featured_image_from_gallery($post_id) {
if (get_post_type($post_id) != array( 'cpt1', 'cpt2')) {
return;
}
$has_thumbnail = get_the_post_thumbnail($post_id);
if ( !$has_thumbnail ) {
$images = get_field('acf_gallery', $post_id, false);
$image_id = $images[0];
if ( $image_id ) {
set_post_thumbnail( $post_id, $image_id );
}
}
}
I usually use save_post hook, $post_id variable, get_post_type and in_array functions.
function set_featured_image_from_gallery($post_id)
{
$included_cpts = array('cpt1', 'cpt2', 'cpt3');
if (in_array(get_post_type($post_id), $included_cpts)) {
$has_thumbnail = get_the_post_thumbnail($post_id);
if (!$has_thumbnail) {
$images = get_field('acf_gallery', $post_id, false);
$image_id = $images[0];
if ($image_id) {
set_post_thumbnail($post_id, $image_id);
}
}
}
}
add_action('save_post', 'set_featured_image_from_gallery');

Update meta field after post save/update on wordpress

I have a custom post type called "professional".
After a Professional is saved (new or update) I need to override the value of a meta field called custom_permalink.
If I use this:
function afterPostUpdated($meta_id, $post_id, $meta_key='', $meta_value=''){
if(get_post_type($post_id) == 'professional' && $meta_key=='custom_permalink') {
if($_GET['message']==1) {
die($meta_value);
}
}
}
add_action('updated_post_meta', 'afterPostUpdated', 10, 4);
The die() only occurs when the field is changed. I need this to work everytime the post is saved.
OK, I found my aswer:
function afterPostUpdated($meta_id, $post_id, $meta_key='', $meta_value=''){
if(get_post_type($post_id) == 'tab_especialidad') {
if($_GET['message']==1) {
$terms = get_the_terms($post_id, 'especialidades');
$esp_slug = $terms[0]->slug;
$post_slug = sanitize_title(get_the_title($post_id));
$custom_permalink = 'especialidades/'.$esp_slug.'/'.$post_slug.'/';
//die($custom_permalink);
update_post_meta($post_id, 'custom_permalink', $custom_permalink);
}
}
}
add_action('updated_post_meta', 'afterPostUpdated', 10, 4);

Change post permalink dynamically via custom field on save/update

I'm wanting to change a WordPress post's permalink (post_name) dynamically every time time a post is saved/updated by pulling from a custom field present in the post and replacing the permalink with this value. I have code within functions.php which is working, except that it appends -2 to the permalink. I assume this is because something is occurring twice, the first time resulting in the permalink I want, and the second resulting in WordPress responding to the "duplicate" by adding -2.
This is the current code:
add_action('save_post', 'change_default_slug');
function change_default_slug($post_id) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
if ( !current_user_can('edit_post', $post_id) )
return;
remove_action('save_post', 'change_default_slug');
wp_update_post(array('ID' => $post_id, 'post_name' =>get_post_meta($post_id,'request_number',true)));;
add_action('save_post', 'change_default_slug');
}
I'll take a crack at this...
Now, this is assuming that you are using ACF for your custom field...
If not, simply update the code with the WP custom meta functions instead. Oh, don't forget the change the field_5fed2cdbc1fd2 with your ACF field Key
add_filter('save_post', 'change_post_name', 20, 1);
function change_post_name($post_id)
{
$post_type = get_post_type();
if ($post_type == "post"){
$acf_title_field = $_POST['acf']['field_5fed2cdbc1fd2']; // get the field data by $_POST
if (!empty($acf_title_field)) {
// update the title in database
$wpdb->update($wpdb->posts, array('post_title' => $acf_title_field, 'post_name' => sanitize_title($acf_title_field)), array('ID' => $post_id));
}
}
}
add_filter( 'wp_insert_post_data', 'update_post_name', 50, 2 );
function update_post_name( $data, $postarr ) {
$post_type = get_post_type();
if ($post_type == "post"){
//Check for the post statuses you want to avoid
if ( !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$acf_title_field = $_POST['acf']['field_5fed2cdbc1fd2']; // get the field data by $_POST
// $data['post_name'] = sanitize_title( $data['post_title'] );
$data['post_name'] = sanitize_title( $acf_title_field );
}
return $data;
}
}

simple condition on wordpress function.php not work

i want add a new field "title" on comment of wordpress, after insert the new input field in a default form of wordpress i added this in my function.php for save the title when new comment are submitted.
this is the code i use for save title:
function add_comment_meta_values($idcommento) {
global $post;
$idcommento= get_comment_ID();
$tipodipost= get_post_type($post->ID);
if( get_post_type($post->ID) == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcommento , 'title_svz', $title, false);
}}
}
add_action ('comment_post', 'add_comment_meta_values', 1);
this code work only when remove the condition :
if( get_post_type($post->ID) == 'service') {}
and i don't understand why, i have already tried this condition in comment.php or in a footer with simple function like this
function test_function() {
if( get_post_type($post->ID) == 'service') { echo 'done'; }
}
add_action( 'wp_footer', 'test_function' );
and it's work, so i don't understand why don't work in my primary code, any idea ?
SOLVED MYSELF
THIS IS THE NEW CODE:
function add_comment_meta_values($idcomment) {
$comment_full = get_comment( $idcomment );
$idpost = $comment_full->comment_post_ID;
$typepost= get_post_type($idpost);
if( $typepost == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcomment , 'title_svz', $title, false);
} }
}
add_action ('comment_post', 'add_comment_meta_values', 10, 1);
Sometimes in Wordpress, depending on the Context, the global $post may give you unexpected result. Thus something like $post->ID may not point to the appropriate ID you are looking for. You may as well try inspecting the $post object to see if it is what you had expected; like so:
<?php
function add_comment_meta_values($idcommento) {
global $post;
// TRY DUMPING THE $post VARIABLE TO SEE WHAT YOU GET
var_dump($post->ID);
var_dump($post->post_type);
var_dump($post);
exit;
$idcommento = get_comment_ID();
$tipodipost = get_post_type($post->ID);
// ALTHOUGH THIS IS ESSENTIALLY THE SAME AS WHAT YOU DID
if( $post->post_type == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcommento , 'title_svz', $title, false);
}}
}
add_action ('comment_post', 'add_comment_meta_values', 1);
After the Inspection, you'd sure know where and what was not OK in your Code...

How to get access to post ID when using save_post action for WordPress

So I have this code that basically adds a post meta field when a mirror is created. The only issue i'm having is how I can access to the post id of the mirror that was just created? I've got the following code from this link: http://codex.wordpress.org/Plugin_API/Action_Reference/save_post that has sample code, and adjusted it to my needs. The mirrors are created on the front end using a form. Using the code below, the post meta field values are not added to the mirrors when they are created. This is probably because $post_id doesn't hold anything.
From the link provided above, it says I have access to $_GET and $_POST, which one should I use and how do I get the ID of the post? Should I just use $_POST->ID?
// Order mirrors when they are created
function order_mirror_create($post_id) {
$slug = 'mirror';
if ( $slug != $_POST['post_type'] ) {
return;
}
if ($videohost == "UploadAnime") {
add_post_meta(get_the_ID(), 'video_display_order', 1, true);
} else {
add_post_meta(get_the_ID(), 'video_display_order', time(), true);
}
}
add_action( 'save_post', 'order_mirror_create' );
EDIT: Updated my code using $post->ID and added global $post, however, this still does not work.
// Order mirrors when they are created
function order_mirror_create($post_id) {
global $post;
$slug = 'mirror';
if ( $slug != $_POST['post_type'] ) {
return;
}
if ($videohost == "UploadAnime") {
add_post_meta($post->ID, 'video_display_order', 1, true);
} else {
add_post_meta($post->ID, 'video_display_order', time(), true);
}
}
add_action( 'save_post', 'order_mirror_create' );
EDIT 2: Defined $videohost and used $post_id, however, still not working.
// Order mirrors when they are created
function order_mirror_create($post_id) {
$slug = 'mirror';
if ( $slug != $_POST['post_type'] ) {
return;
}
$videohost = get_post_meta($post_id, 'video_provider', true);
if ($videohost == "UploadAnime") {
add_post_meta($post_id, 'video_display_order', 1, true);
} else {
add_post_meta($post_id, 'video_display_order', time(), true);
}
}
add_action( 'save_post', 'order_mirror_create' );

Categories