understanding add_post_meta WordPress method? - wordpress

could any one explain for me the add_post_meta WordPress method, I have read the codex but still I do not understand the method ?
I wrote this example, but the output was just number, I do not know what that number is and I do not know how this method could help me.
add_shortcode("haidar",'run_haidar');
function run_haidar($attr, $content)
{
$meta = add_post_meta( 90, 'my_key', 'The quick, brown fox jumped over the lazy dog.' );
return $meta;
}
clear example would be appreciated.
thanks every body

The number you're referring to is probably the return value (0/1 - true/false) which you don't need. There are a number of flaws with your original example including setting the result of add_post_meta to a variable titled $meta and returning a non-existent variable, $m.
Here's what I think you're attempting to do:
function run_haidar( $attr, $content ) {
$meta = get_post_meta( 90, 'my_key', true );
return $meta;
}
add_shortcode( 'haidar','run_haidar' );
add_post_meta sets the meta value whereas what you need to do is get it. You'll use get_post_meta instead - http://codex.wordpress.org/Function_Reference/get_post_meta
This will get the meta value of 'my_key' on post 90 and output it in the shortcode. Go into post 90 and set a value for my_key.

Related

Wordpress search database values and return

i'm have been searching for a code that will do some thing like this in WordPress and but it CANT call on woocommerce
nothing seems to work
if
get_post_meta( get_the_ID(), '_regular_price' is greater then 1)
do this
else
do this code
It depends on the place where you are using this function.
Imagine you are trying to edit a product page. In your woocommerce templates folder, you find for example: price.php
Usually there is one global variable available already, and if it's not available you can set it with global $product. With $product->get_id() you can get the product id then.
With the global variable the sale price is then available like this $price = $product->get_sale_price();
In order to make the if statement, you need the data so then the next step. Maybe your price is empty for some reason, which returns undefined, making it difficult to do the if statement
if (empty($price))
$price = 0;
if($price > 1))
// do your thing
$my_post_meta = get_post_meta( get_the_ID(), 'sale_price', true);
if ( ! empty ( $my_post_meta ) ) {
do code here
} else
do other code
this works for me

WP Rest API get post by slug with special characters (ex: !&')

I have posts with slugs with special characters. One of them is the following:
http://localhost/wp-json/wp/v2/posts?slug=my-post!
Unfortunately, WP REST API not showing the content since it has (!) within the slug.
Is there any solution you guys would recommend?
I have found the solution (at least for my case), not sure if it'll work for you but may indicate you the way to go. We need to tap into the function used to sanitize the slugs, as I said in my comment, by default is wp_parse_slug_list. Looking at the code the function that actually sanitizes the slug is sanitize_title.
Looking at the source code, wp_parse_slug_list calls sanitize_title with only one argument, which means that the context used is save. This means that, for posts that were already saved without being sanitized by this function, the slug won't match and the post will be inaccessible through the API. The solution is to change the sanitizing function slightly by adding a filter:
add_filter('rest_post_collection_params', function($query_params) {
$query_params['slug']['sanitize_callback'] = 'sanitize_rest_api_slug';
return $query_params;
}, 20, 1);
function sanitize_rest_api_slug( $list ) {
if ( ! is_array( $list ) ) {
$list = preg_split( '/[\s,]+/', $list );
}
foreach ( $list as $key => $value ) {
$list[ $key ] = sanitize_title( $value, '', 'query' );
}
return array_unique( $list );
}
The filter is actually being applied on the function get_collection_params() on the class-wp-rest-posts-controller class, but if you look at the source code, the filter has a variable depending on the post_type, so if you have another special kind of posts defined (besides post) you need to add/change a filter for that kind as well.
I hope this helps somebody else like me, even if it's too late for your issue.

Wordpress how to retrieve aguments from filter

lets say this filter for example:
apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
I will hook on it like this:
function my_func($wut,$huh){
//...my code...
}
add_filter('wp_generate_attachment_metadata', 'my_func' );
How can I access the $metadata and $attachment_id from the initial apply_filter call in my function?
Thank you #Samvel for your comment, that totally works.
As seen on this page: https://developer.wordpress.org/reference/functions/add_filter/ I needed to add that 4th argument in my add_filter and set it to 2 to allow a second argument to pass in, otherwise it was locked at 1. Thats why I couldnt retrieve that second argument!

What's the difference between global var post and get_post?

I use the_posts filter to add an object to each queried post. When access the added object, I get different result by using $post or get_post.
This is the code to attach the object to posts:
add_filter( 'the_posts', 'populate_posts_obj', 10,2 );
function populate_posts_obj( $posts, $query ){
if ( !count( $posts ) || !isset($query->query['post_type']) )
return $posts;
if( in_array( $query->query['post_type'], get_valid_grade_types())){
foreach ( $posts as $post ) {
if ( $obj = new Gradebook( $post->ID ) )
$post->gradebook = $obj;
}
}
return $posts;
}
Then, access the obj via $post, sometimes get the obj, sometimes not (even when it's the same post):
function get_the_gradebook(){
global $post;
return isset($post->gradebook) ? $post->gradebook : null;
}
Access the obj via get_post(), always get the obj:
function get_the_gradebook(){
global $post;
$p = get_post($post->ID);
return isset($p->gradebook) ? $p->gradebook : null;
}
I can just use the get_post() version, but it would be useful if I know why the difference.
Additional info:
If you ask the reason I attach an obj to each post, I think WordPress may take care of the caching process at the first place. Then, other caching plugins can work on my obj as if working on standard WP posts.
Lets explain you with a little bit pseudo code. I am trying to be broad with my approach so that my answer is relevant to StackOverflow however I still don't know how many down votes I may be receiving for this.
The simple difference is $post is a variable and get_post() is a method that means you can expect a different output from get_post() due to several dependencies however $post will only change when you explicitly do that.
Lets assume something like this
function get_post() {
return rand(0, 5);
}
$post = get_post(); /* lets assume random
value that was generated
this time was "2" */
Now each time you call get_post() its value be keep changing however the value of $post is always 2.
Coming back to the context of wordpress, $post is set using get_post() within the Loop and corresponds to the object referring to default post ID for current URL where as get_post() will take post ID as an input and return the post object.
$post is what WordPress considers to be the current "post" (post/page/custom post type) and can quite often end up giving you data you didn't quite expect. This is especially true if you perform WP_Query's in your template or have a template that uses data from several "posts".
By using get_post() with the ID you want the data from, you can be assured that you are getting the data you really want.

wordpress plugin creation get_post_meta

I am building my first plugin, and I am using as a reference the following link.
http://www.sitepoint.com/create-a-voting-plugin-for-wordpress/
and I am trying to underestand the following part of the code:
function voteme_addvote()
{
$results = '';
global $wpdb;
$post_ID = $_POST['postid'];
$votemecount = get_post_meta($post_ID, '_votemecount', true) != '' ? get_post_meta($post_ID, '_votemecount', true) : '0';
$votemecountNew = $votemecount + 1;
update_post_meta($post_ID, '_votemecount', $votemecountNew);
$results.='<div class="votescore" >'.$votemecountNew.'</div>';
// Return the String
die($results);
}
I run the code and it works, but I just dont understand the following:
What is "get_post_meta" doing?
Does it create a custom meta field, the same as add_post_meta?, if it doesnt why there is not an add_post_meta?
I checked the DB, and it looks like it is creating a custom meta field... so in that order what is the difference between get_post_meta and add_post_meta?
Thanks very much for helping me understand this.
The first time your code runs, get_post_meta returns '' so $votemecount is set to 0. The following update_post_meta creates the new meta field as documented below. Values that start with _ are not displayed (are hidden meta fields).
The function, update_post_meta(), updates the value of an existing meta key (custom field) for the specified post.
This may be used in place of add_post_meta() function. 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.

Resources