get_post_meta - WordPress - php

This code is designed to add a button to specific posts using the get_post_meta function. How do I alter the get_post_meta function to display this button on a specific post? I have already tried changing its $post->ID parameter to '1464', which is the post ID I want to use.
function custom_listify_single_job_listing_actions_after() {
global $post;
$url = get_post_meta( $post->ID, 'your_custom_meta_key', true );
echo 'My Button';
}
add_filter( 'listify_single_job_listing_actions_after', 'custom_listify_single_job_listing_actions_after' );

If you only want to run this code on a specific post, you need to add an if statement to check for that post ID.
Your code would need to look similar to this:
if($post->ID == 1464){
$url = get_post_meta( $post->ID, 'your_custom_meta_key', true );
echo 'My Button';
}
This simply wraps the get_post_meta() function and echo statement so that both of these only run on the post you want them to. Any other post will ignore the code.

Related

Auto tag custom posts in Wordpress

I am looking for a way to auto tag custom posts in Wordpress without using a plugin.
I have a custom post type 'tech-video' and I want to auto tag it with the video tag every time a post gets published of that type.
I tried this code snippet but it doesn't work:
/* Auto Tag Tech Videos */
add_action('publish_tech_video', 'tag_tech_video', 10, 2);
function tag_tech_video($post_id, $post){
wp_set_post_terms( $post_id, 'tech-video', 'video', true );
}
I'm not skilled with either PHP or Wordpress hooks so any help is appreciated.
Thank you,
You're close; You just got the hook name wrong.
Whenever a post is saved, the following is run:
do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
To leverage this hook you can run:
add_action('save_post_tech_video', 'tag_tech_video', 10, 2);
function tag_tech_video($post_id, $post){
// check the term has not already been added.
$terms = wp_get_post_terms($post->ID, 'video');
$term_names = array_map(function($term){return $term->name;},$terms);
if(!in_array('tech-video',$term_names){
wp_set_post_terms( $post_id, 'tech-video', 'video', true );
}
}
But note: Since the "save_post" hook is run every time the post is saved, you need to check that the term has not already been added.
Note that the signature for wp_set_post_terms is:
function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false );
So this assumes that you have a registered taxonomy named "video", and the taxonomy is linked to the "tech_video" post type.
After doing some more digging into the Wordpress Codex I was able to figure out a more elegant solution that works great:
// Call when the post gets created
add_action('save_post_tech-video', 'tag_tech_video', 10, 2);
function tag_tech_video($post_id, $post) {
// If the video tag doesn't exist, add it
if (!has_tag('video', $post->ID)) {
wp_set_post_tags( $post_id, 'video', true );
}
}
Note that I had to change tech_video to 'tech-video' to make it match the name defined by the Custom Post Type (and thus call properly).
I like this method because it's cleaner.
Thanks #andrew for pointing me in the right direction at least!

Return URL query into post shortcode from functions.php in Wordpress

I am able to get the shortcode to return the string 'evs' inline in post [ when I plug that in to return ] but unable to return the post thumb url, using the same shortcode from functions.php
What am I doing wrong here:
function thumburl_func( $atts ){
$url = get_the_post_thumbnail_url('large');
// $eva = 'evs';
return strval($url);
}
add_shortcode( 'thumburl', 'thumburl_func' );
Been stuck on this for ages now - please save my week !
You are almost there, but you are using get_the_post_thumbnail_url incorrectly.
The size of the image you want is the second argument into the function, the first argument is the post id. Although the post id is optional, if you want to pass in the size, you need include something as the first argument.
If you are in the Wordpress "loop" and the global $post is set up, you can simply pass in null e.g.
$url = get_the_post_thumbnail_url(null, 'large');
However, as this is for a shortcode, the best option is to explicitly pass in the post id - this means the shortcode will work even outside the loop (e.g. in a widget).
function thumburl_func( $atts ){
// 1. get the post id of the current post
$post_id = get_queried_object_id();
// 2. pass the post is into get_the_post_thumbnail_url
$url = get_the_post_thumbnail_url($post_id, 'large');
return $url;
}
add_shortcode( 'thumburl', 'thumburl_func' );

Wordpress hook to edit the post meta data before displaying a post edit page

I'm trying to edit a field in the meta data of a post before it is being displayed on the screen.
I have been looking at the 'load-post.php' hook, but this is called before the post is loaded (if I've understood that correctly), so the post id and meta data are null.
I've tried other hooks, but I haven't been able to make this work.
The following post meta field needs to be changed before it is displayed on the edit page.
$post_price = get_post_meta(get_the_ID(), 'price', TRUE);
Example: Price = 10 in the database, but I want it to be Price = 15 when it is displayed on the post edit page.
Any links, tips and ideas are much appreciated. :)
Edit:
My current solution:
add_action('load-post.php','calculate_price');
function calculate_price(){
$post_id = $_GET['post'];
//get price from post by post_id and do stuff
}
Is this the correct way?
The best hook I found is load-post.php using $current_screen.
For Woocommerce product, that works :
add_action('load-post.php', "calculate_price" );
function calculate_price( ){
global $current_screen;
if( is_admin() && $current_screen->post_type === 'product' ){
$post_id = (int) $_GET['post'];
$post = get_post( $post_id );
//Do something
}
}
EDIT: okay I thought you just need to work with ID of post. If you need to change post objects (already loaded from db and ready to be printed), you can use 'the_post' instead. Since you just need to access to post id, I would do something like this:
function my_the_post_action( $post ) {
$screen = get_current_screen();
if( is_admin() && $screen->parent_base == 'edit' && get_post_type() == 'product' ) {
$post_id = $post->ID;
$price = (int) get_post_meta( $post_id, 'price', true );
update_post_meta( $post_id, 'price', $price + 5 );
}
}
add_action( 'the_post', 'my_the_post_action' );
This part:
get_post_type() == 'product'
is not necessary, but you should determine for which kind of post (based on post type, category, metafield etc.) you want to run this piece of code. Without it will be executed everytime in admin query. This code is not tested, if something is wrong feel free to report.

Gravity form auto populating field not working

I am using gravity forms for wordpress and and i have following function to auto populate the form field:
add_filter('gform_field_value_vendor_category', 'populate_post_vendor_category');
function populate_post_vendor_category($value){
global $post;
$vendor_category = the_terms( $post->ID, 'listing_category');
return $vendor_category;
}
Adding the parameter name vendor_category to the form does not seem to work.
I have tried the following code on a template file and it displays the current listing category.
<?php global $post;
$vendor_cat = the_terms( $post->ID, 'listing_category');
echo $vendor_cat; ?>
Not sure why the field is not auto populating?
the terms is for echoing . Use get_the_terms() instead for returning values.
get_the_terms( $id, $taxonomy );
as a rule of thumb, when you do not see the prefix get_ in wordpress, the function will echo on the screen. like the_title() ( echoing ) and get_the_title (returning )..

Add Genesis PHP line to post_info

I have a PHP statement
<?php echo get_post_meta( $post->ID, 'jetpack-post-views', true ); ?>
This i used by a wordpress plugin called "Jetpack post views"
It asked me to place the provided code anywhere on the page to see the post views.
I want it on "genesis_entry_header" with the other post_info
I don't know how to do that, and I look everywhere for a solution before I post this.
Thank you
Try with this,
function wp_87978_jpv(){
global $post;
echo get_post_meta( $post->ID, 'jetpack-post-views', true );
}
add_action('genesis_entry_header', 'wp_87978_jpv');

Categories