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);
Related
Below I have created a custom permalink tag for my posts, it works as expected and retrieves the parent category and puts it in the URL but for some reason all my posts are giving out 404s and I cant figure out why I have flushed all caches tried saving the permalinks but nothing works.
Not sure if there is problem with the code that's stopping it from working?
add_filter('post_link', 'region_cat_permalink', 10, 3);
add_filter('post_type_link', 'region_cat_permalink', 10, 3);
function region_cat_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%region%') === FALSE) return $permalink;
$post = get_post($post_id);
if (!$post) return $permalink;
$terms = get_the_category($post->ID);
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $parentterm = get_cat_name($term[0]->parent);
else $parentterm = 'eu';
return str_replace('%region%', $parentterm, $permalink);
}
Go Wordpress Admin => Settings => Permalinks
Select Custom Structure and add this
/%category%/%postname%/
Then go Wordpress Admin => Settings => Writing
And select eu as default category
Then delete your function
Update
add_filter('post_link', 'region_cat_permalink', 10, 3);
add_filter('post_type_link', 'region_cat_permalink', 10, 3);
function region_cat_permalink($permalink, $post_id, $leavename) {
//print_r($permalink);
$post = get_post($post_id);
if (!$post) return $permalink;
$terms = get_the_category($post->ID);
if($terms[0]->parent > 0) {
$term_name = $terms[0]->slug;
$term_pattern = $term_name . '/';
return str_replace($term_pattern, '', $permalink);
} else {
return $permalink;
}
}
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.
Here's my code at functions.php and still can't set as featured image in the post type of the blog.
add_action('acf/save_post', 'acf_set_featured_image');
function acf_set_featured_image($post_id){
$value = get_field('daily_selfie', $post_id);
if($value != ''){
add_post_meta($post_id, '_thumbnail_id', $value);
}
return $value;
}
Try this
function acf_set_featured_image( $value, $post_id, $field ){
if($value != ''){
//Add the value which is the image ID to the _thumbnail_id meta data for the current post
add_post_meta($post_id, '_thumbnail_id', $value);
}
return $value;
}
// acf/update_value/name={$field_name} - filter for a specific field based on it's name
add_filter('acf/update_value/name=foo', 'acf_set_featured_image', 10, 3);
function awepop_show_views($singular = "view", $plural = "views", $before = "This post has: ")
{
global $post;
$current_views = awepop_get_view_count();
$views_text = $before . $current_views . " ";
if ($current_views == 1) {
$views_text .= $singular;
}
else {
$views_text .= $plural;
}
return $views_text;
}
function awepop_append_to_meta($meta){
return $meta[] = awepop_show_views();
}
add_filter( 'the_meta_key', 'awepop_append_to_meta' );
I am trying to embed post views into post meta. I search alot but couldn't find the appropriate filter for post meta. please advice how can i embed my view count into post meta
Try hooking to init and using update_post_meta. For example, this is how you could increment your post views:
function my_update_postmeta() {
global $post;
update_post_meta( $post->ID, my_meta_key, $my_meta_value + 1 );
}
add_action( 'init', 'my_update_postmeta' );
Note: You would put my_update_postmeta() in your page or post template.
Ref: http://codex.wordpress.org/Function_Reference/update_post_meta
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' );