i am using following get_post_meta function but it is not showing any values, it is showing blank result
$meta_values = get_post_meta( $post_id, 'salary', true );
I am able to see the value present in phpmyadmin .I am using it in a plugin in action(save_post,sftoproject), but it is showing any value in my code.
Here's my code
add_action('save_post','user_sf_project');
function user_sf_project($post_id){
$SOAP_CLIENT_BASEDIR = plugin_dir_path(__FILE__).'Force.com-Toolkit-for-PHP/soapclient/';
require_once ($SOAP_CLIENT_BASEDIR.'/SforcePartnerClient.php');
require_once ($SOAP_CLIENT_BASEDIR.'/SforceHeaderOptions.php');
require_once ('Force.com-Toolkit-for-PHP/Samples/userAuth.php');
try {
$mySforceConnection = new SforcePartnerClient();
$mySoapClient = $mySforceConnection->createConnection($SOAP_CLIENT_BASEDIR.'/partner.wsdl.xml');
$mylogin = $mySforceConnection->login("username", "password");
$postdata = get_post($post_id);
$meta_values = get_post_meta( $post_id, 'salary', true );
$fields = array (
'Name' => $post_id,
'First_Name__c' => $postdata->post_title,
'Salary__c' => $meta_values,
);
$sObject = new SObject();
$sObject->fields = $fields;
$sObject->type = 'NewObject__c';
$createResponse = $mySforceConnection->create(array($sObject));
print_r($createResponse);
$ids = array();
foreach ($createResponse as $createResult) {
print_r($createResult);
array_push($ids, $createResult->id);
}
} catch (Exception $e) {
echo $mySforceConnection->getLastRequest();
echo $e->faultstring;
}
From the WP docs:
It is a wrapper for get_metadata('post'). To return all of the custom
fields, see get_post_custom().
You must use get_post_custom() function (not get_post_meta) for custom post types, but I highly recommend that you always use get_metadata instead of get_post_meta() and get_post_custom() to avoid simple problems...
Example for get_metadata():
get_metadata('your_custom_post_type', $post_id, 'salary', true)
Related
I am creating a Playlist using LearnDash Plugin,
Below is my code, This code works good but I can't get a video of a particular Lesson
Is there anyone who knows how to fix this?
add_shortcode( 'playlist', 'playlist_for_LMS' );
function playlist_for_LMS() {
global $post;
$user_id = get_current_user_id();
$courses = learndash_user_get_enrolled_courses( $user_id, array(), true );
$lessons = learndash_get_course_lessons_list( $courses[0], $user_id, array(), true );
foreach ($lessons as $lesson){
$postID = $lesson['post']->lesson_video_url;
$postImage = get_the_post_thumbnail($lesson['post']->ID);
$postTitle = $lesson['post']->post_title;
$postLink = get_post_permalink($lesson['post']->ID);
echo ''.$postTitle.' '.$postID.'<br>';
}
}
You use the below code and whats are the your output and go through your out put of fetched data. And also you can get errors if you have.
echo '<pre>';
print_r($lessons);
I have a post_save hook that manages setting the title of a particular post type. Within that logic I need to retrieve the post's categories. However, the categories are not yet saved at the time the post_save triggers for the first save of a new post.
add_action('save_post', 'save_report');
function save_report($post_id) {
$data = get_post($post_id, ARRAY_A);
if($data['post_type'] == 'report') {
$date = get_post_meta($post_id)['date'][0];
// I need to get the category of the post on the first time the post is saved
// $categories only gets a value AFTER the first save
$categories = get_the_terms($post_id, 'report_type');
$cat_string = '';
foreach($categories as $value) {
$cat_string .= $value->slug;
}
$new_title = date('m/d/Y', strtotime($date)).' '.$cat_string;
remove_action('save_post', 'save_report');
wp_update_post(array(
'ID' => $post_id,
'post_title' => $new_title
));
add_action('save_post', 'save_report');
}
}
Because of this, I have to save the post twice to get it to change the title to what I want. Is it possible to get the new category of the post as it is being saved like this?
Not quite sure what you're using the categories for after the fact, but you can just retrieve them after the wp_update_post runs.
add_action('save_post', 'save_report');
function save_report($post_id) {
$data = get_post($post_id, ARRAY_A);
if($data['post_type'] == 'report') {
$date = get_post_meta($post_id)['date'][0];
$new_title = date('m/d/Y', strtotime($date)).' '.$cat_string;
remove_action('save_post', 'save_report');
/*
* return the updated post ID (which is the same really as your
* initial ID, so you could just use that. But for demonstration it helps
* to see it logically.)
*/
$updated_post_id = wp_update_post(array(
'ID' => $post_id,
'post_title' => $new_title
));
$categories = get_the_terms($updated_post_id, 'report_type');
$cat_string = '';
foreach($categories as $value) {
$cat_string .= $value->slug;
}
add_action('save_post', 'save_report');
}
}
An alternative, if you're both creating and updating posts programatically is to use wp_insert_post, which covers both new creation (if you don't pass an ID), and updating (if you do pass an ID).
You could also create a separate function to retrieve IDs that you could use with any number of other functions you run.
function retrieve_report_cats($post_id) {
cats_list = array();
$categories = get_the_terms($post_id, 'report_type');
foreach($categories as $value) {
$cat = $value->slug;
$cats_list[] = $cat;
}
return $cats_list;
}
Im extremely new to WordPress and WooCommerce code. I've been provided with some working code that resides in the plugins/woocommerce/templates/archive-product.php
The function is pretty simple, it simply fetches for an array of data from a remote site and makes use of the JSON returned to find the matching SKUs and inject them as items in the product list.
Works quite nicely, however, as I'm new to Woo & WP, I'm hoping someone might be able to show me how I can transform this code into the proper way of it being defined as a plugin?
I'm hoping its just a case of wrapping some additional code around the function, but I'm unsure as to where to start
any tips greatly appreciated
if ( wc_get_loop_prop( 'total' ) ) {
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$cat = explode('/', $actual_link);
if ($cat[4] == 'my-product-list') {
global $current_user;
get_currentuserinfo();
$data = array( 'email' => $current_user->user_email);
$response = wp_remote_post( 'https://www.shop.com/remote-data/', array( 'data' => $data ) );
$curl = 'https://www.shop.com/remote-data/';
$response = wp_remote_get( $curl );
$rows = wp_remote_retrieve_body( $response ) ;
$decode = json_decode(stripslashes($rows), true);
global $wpdb;
$product_id = array();
$i = 0;
foreach ($decode as $single_data) {
foreach ($single_data['items'] as $data) {
$result = $wpdb->get_results ( "SELECT post_id FROM wp_postmeta WHERE meta_key = '_sku' AND meta_value = '".$data."'" );
$product_id[$i] = $result[0]->post_id;
$i++;
}
}
$product_id = array_filter(array_unique($product_id));
$args = array(
'post_type' => 'product',
'post__in' => $product_id
);
//The Query
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
$the_query->the_post();
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
You could try to hook it into woocommerce_before_main_content
Like so:
function fetch_shop_data() {
// your code
}
add_action('woocommerce_before_main_content', 'fetch_shop_data');
I hadn't realised that WordPress had added a native function for it to the Codex, but for some odd reason the page is blank. Does that mean the functionality is still coming or the page was added by mistake?
http://codex.wordpress.org/Function_Reference/get_permalink_by_slug
It's blank because it doesn't exist. If you change that function name to any random name you like you'll still see a blank page (unless of course the name you change it to is a real function).
function get_permalink_by_slug( $slug, $post_type = '' ) {
// Initialize the permalink value
$permalink = null;
// Build the arguments for WP_Query
$args = array(
'name' => $slug,
'max_num_posts' => 1
);
// If the optional argument is set, add it to the arguments array
if( '' != $post_type ) {
$args = array_merge( $args, array( 'post_type' => $post_type ) );
} // end if
// Run the query (and reset it)
$query = new WP_Query( $args );
if( $query->have_posts() ) {
$query->the_post();
$permalink = get_permalink( get_the_ID() );
} // end if
wp_reset_postdata();
return $permalink;
}
I have 2 custom content types created by Pods CMS: fights and events. In the fights content type there is a relationship field for the event. How would I select all fights from the database by a specific event id? I tried querying the pods relationships table manually but that gave me incorrect results.
$fights = pods( 'fights' );
$params = array(
'where' => 'event.id = 3'
);
$fights->find( $params );
// loop through a while( $fights->fetch() ) and use $fights->field() to get the values of each field
That should do it, but you'll want to look at the 'find' documentation for your specific content type case as event.id may not be what you want (you may want event.ID).
http://pods.io/docs/code/pods/
http://pods.io/docs/code/pods/find/
http://pods.io/docs/code/pods/field/
http://pods.io/docs/code/pods/display/
Using this function you can easily display all individual relational fields with shortcode:
function get_pod_fields($atts) {
$a = shortcode_atts( array('field' => '', 'pod'=> '', 'relation'=> '', 'type'=>'', 'as'=>'url'), $atts );
$pod = pods( $a['pod'], get_the_id() );
$related = $pod->field( $a['relation']);
$field = get_post_meta( $related['ID'], $a['field'], true );
if($a['type'] == "image") {
if($a['as'] == "image") {
$field = '<img src="'.$field = $field['guid'].'"></img>';
} else {
$field = $field['guid'];
}
} else {
$field = $field;
}
return $field;
}
add_shortcode( 'podfields', 'get_pod_fields' );
[podfields field="profile_picture" type="image" pod="therapy" as="image" relation="therapist"]