I'm trying to access some fields that have been assigned to a post via a plugin named Snax.
You can see here in the admin that "Story" has being assigned to a post as a "Snax Format":
But when I check the $post object inside the loop on the front end using something like $my_post = get_post(); var_dump($my_post); I can't see any anything relating to snax or "story".
What php can I use to access that post data on the front end inside the loop?
I'm asking because I want to use that field data inside my post template.
I think the plugin will store they data as meta_values. You can retrieve this data with get_post_meta()
$meta_data = get_post_meta( get_the_ID() );
var_dump( $meta_data );
Related
I have a Wordpress installation with Woocommerce and I added a ACF field to the orders. It's called 'booking_reference'.
Inside my functions.php file, on checkout I update the 'booking_reference' custom field. It gets saved to the database. All is working as it should.
In my next function i'm trying to email this custom field to the customer. I use this code:
$order_id = $order->get_id();
$reference_number_acf_order = get_field('booking_reference', $order_id );
When I echo $reference_number_acf_order, it's empty. The $order_id variable is correct. It contains the order correct order ID.
The rest of the email to the customer is correct, it contains data from the order. Only the ACF value is empty.
Are u using WP_Query() or something like in your new function?
If so. try calling wp_reset_query(); first:
wp_reset_query();
$reference_number_acf_order = get_field('booking_reference', $order_id );
I'm working on a project where I upload a file and use its path in a shortcode. Right now I've hard-coded the post's ID into my code but I want to make it dynamic so that new posts automatically get the correct shortcode.
<?php
global $wpdb;
$thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = 5574" ) );
echo do_shortcode ('[sgpx gpx="'.'/wp-content/uploads/' . $thepost->meta_value . '"]');
?>
Going off of what #Damocles said, you are really setting yourself up for trouble with your current path. Instead, there are built-in WordPress functions that abstract away the database and utilize caching that you are strongly encouraged to use.
// Get the current WordPress post
$the_post = get_post();
//If we have a value (there are cases where this will be empty)
if($the_post){
// Get the value from the post meta table by the current post's ID
$the_post_meta = get_post_meta($the_post->ID, 'YOUR_META_KEY_HERE', true);
// Double-check that we have a value
if($the_post_meta) {
// Finally, echo the shortcode
echo do_shortcode ('[sgpx gpx="'.'/wp-content/uploads/' . $the_post_meta . '"]');
}
}
There are several ways to get the current post however get_post() is the most common. If you are in a custom loop, however, you might need to adjust accordingly.
To access the meta, use get_post_meta() which includes some optimizations including use a cache instead of the database.
Although there are definitely exceptions, generally speaking, if you are working with WordPress and you find yourself writing SQL statements, there is almost always a better, safer, faster, etc. way to do it using core functions.
I need help in Advanced custom field pro plugin
i want to display 3 post of credit taxonomy to the home page
i had edited my field group like :
Field Label : home credit
Field Name : home_credit
Field Type : Post object
Filter by taxonomy : credit
Now, when i print the data to the homepage by writing this :
- get_field('home_credit', get_the_ID()) );
i am not getting the value from the post object or how to retrieve its value please help. I had also try to debug the value by print_r but no value returning.
if you need fetch data on taxonomy page by ACF,
<?php the_field('home_credit','credit_' . term_id); ?>
Looks like the ID you are using to retrieve the data is not correct. You might have to check what "get_the_ID()" returns to make sure you are using the correct page ID. One solution would be to use "get_queried_object_id()" instead. Try :
$home_credit = get_field( 'home_credit', get_queried_object_id() );
Basically get_queried_object_id() will get the ID of the object queried in the current wp_query. get_the_ID() might fail if post data is not set.
In wordpress, I am showing a count# of posts based on various custom taxonomies. I have retrieved this count using wp-query->found_posts used in functions.php file (used tax-query). I want to have this count number as hyperlink to show details of the posts. Any suggestion on how to get this done?
I first tried to pass the WP_Query object to the php which contains the post data but obj->found_posts did not work in the php file.
You will need to capture posts' IDs from within the WP_Query you're running.
Example:
$post_ids = array();
while ( $wp_query->have_posts() ) : $wp_query->the_post();
$post_ids [] = get_the_ID() ;
endwhile;
Later on, you can use these IDs however you want.
I am using add post meta function to save some data and its not working
<?php
//include '../../../wp-blog-header.php';
$unique = "true";
$pageID = $_GET['postID'];
echo "pageID:";
echo $pageID;
echo "</br>";
$num_posts = $_GET['num_posts'];
echo "num_posts: ";
echo $num_posts;
echo "</br>";
$num_posts_meta_key = "num_posts";
add_post_meta($pageID, $num_posts_meta_key, $num_posts , $unique) or update_post_meta($pageID, "num_posts" , $num_posts);
?>
Can someone help me out?
In first page I am getting all values from textboxes or checkboxes in javascript and then i am passing it in URL to next page where add_post_meta function is there.
I tried using method POST ...but then it doesnt work for me. It just submit the page and come back w/o doing anything on 1st page. I tried with GET method..but nothing works.
Hence I decided to take all values like num of post, post id in javascript and then from there pass it with url by using window.location.
I am very new to wordpress plugin coding. I thought POST method in my plugin is conflicting with some other post method in post.php..not sure though..
I am writing plugin for admin panel.
not sure what your problem is.. are you sure you're passing the right postID parameter? does the post exist in the database?
You don't really need to do add_post_meta() or update_post_meta.
From the manual:
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.
<?php
// This minimum code should work, though you should really check that a post
// with this id does exist.
update_post_meta($_GET['postID'], "num_posts" , $_GET['num_posts']);
?>