Simple Wordpress problem - get_post_meta is not retrieving custom field values. Here's the code that is pulling from the custom fields:
<img src="<?php echo FCG_PLUGIN_URL; ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, 'slider_image', true); ?>&h=250&w=400&zc=1" alt="<?php echo $post_title; ?>" />
In production, this is the HTML I get:
<img alt="Post Title" src="http://***.com/wp-content/plugins/jquery-slider-for-featured-content/scripts/timthumb.php?src=/&h=50&w=80&zc=1">
You can see the src= point in the string is empty - as if there is nothing posting from it. I have isolated and echo'd just the get_post_meta and it's a whitespace. I am 100% sure it's named correctly within the post - is there something glaring I'm missing here?
If you are calling get_post_meta inside the loop then you should call get_post_meta(get_the_id(), 'YOURKEY', true) instead of get_post_meta($post->ID, 'YOURKEY', true)
Strange things happens when you call get_post_meta inside a loop. In some themes developers hack the $post at the beginning and get_post_meta stops working so this is one of the solution for those particular cases too.
Search for the term "slider_image" in the wp_posts and wp_postmeta tables using phpmyadmin. Then view the row that has it to see if there's anything inside.
Also try changing the name of the custom value as a test and see if that works. I use this exact code to do something similar to you and it works:
<p><img src="<? bloginfo('template_url'); ?>/img/downloadresume.png"></p>
Its because of auto save.
use these lines for preventing auto save and user privileges.
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
You could also use get_post_meta( $loop->post->ID, 'yourkey', true ); if you are using $loop = new WP_Query( $args ); or something similar.
Actually, it gave you '/', which is not nothing. I'd say that's what's saved as 'slider_image'. Check the post (or the database directly).
I've written some simple templating functions that enable you to use the meta data (custom data) in your theme. You can write a template function for any meta data key/value pair, and render it in a theme file like so:
<?php the_meta_templates($meta_data_keys) ?>
<?php the_template_for($meta_data_key) ?>
Feel free to check out the basic functions from github and give them a try. You'll need to add them to your themes functions.php file.
<?php get_post_meta(get_the_id(), 'YOURKEY', true) instead of get_post_meta($post->ID, 'YOURKEY', true) ?>
Works for me!
could it be linked to the bug
#18210 (Update_post_meta is case insensitive on meta_key, but get_post_meta is NOT) – WordPress Trac
https://core.trac.wordpress.org/ticket/18210
It would explained the different experiences, depending on db_collation... (forgive me if it total nonsense, I am a newbie .. )
WordPress Database Charset and Collation Configuration | hakre on wordpress
http://hakre.wordpress.com/2010/12/26/wordpress-database-charset-and-collation-configuration/
<?php
// Get custum fields and values
$mykey_values = get_post_custom_values('my_key');
foreach ( $mykey_values as $key => $value ) {
echo "$key => $value ('my_key')<br />";
}
?>
Related
Ok, so I've been trying to figure this out for a while. I'm not exactly a PHP developer but can usually figure things out be looking at examples and Wordpress docs. However I'm struggling.
I'm trying to add a "simple" bit of code to a Wordpress template file, but it won't work. Basically I want to show content on certain pages and different content on other page. This is what I have so far:
<?php if(is_page('123')): ?>
This text
<?php endif; ?>
<?php if(!is_page('123')): ?>
That text
<?php endif; ?>
Any help would be so greatly appreciated.
Are you using this inside the WP Loop? If yes, note this:
Due to certain global variables being overwritten during The Loop,
is_page() will not work. In order to call it after The Loop, you must
call wp_reset_query() first.
(from: https://developer.wordpress.org/reference/functions/is_page/)
Also, you shouldn't put the page ID in quotes - it's an integer.
You should provide more context. What does the provided code echo? Does it always say "That text" although you think that it should say "This text"?
Perhaps too obvious but is '123' the name of the post?
If you want to check for the Post with the ID you should try this:
<?php if(is_page(123)): ?>
This text
<?php endif; ?>
<?php if(!is_page(123)): ?>
That text
<?php endif; ?>
Using '123' makes it a string and searches for a post with a title or slug with that string.
You shouldn't need to Logical "Not" Operator between the two statements. A simple if/else would suffice. That said:
The is_page() function will return false if the current query isn't set up for an existing page on the site.
Possible Conditions:
The global query has been modified/overwritten. If you're using query_posts() before this code - stop! Use WP_Query() along with wp_reset_postdata().
The record with the name '123' isn't actually a page. is_page specifically checks for the page post type. Consider the sister functions is_single() or is_singular().
In that same vein, are you looking for ID: 123? If so, remove the quotes. ID's should be passed as an integer: 123.
You're using this in "The Loop", where it can have unexpected interactions. You'll instead want to use another qualifying statement such as if( get_the_ID() == 123 ) ){ or if( $post->ID == 123 ){.
This code is hooked to an action hook too early such as plugins_loaded where the global query and post objects may not be set up yet.
Check to make sure whether any of those conditions apply, and then you can modify your code to be a bit more succinct:
if( is_page( 123 ) ){
echo 'This is page ID 123';
} else {
echo 'This is NOT page ID 123';
}
Or even further with the Ternary Operator
echo is_page( 123 ) ? 'This is Page ID 123' : 'This is Not page ID 123';
you could try this:
global $post;
<?php if( $post->ID == 346) { ?>
<!-- do your stuff here -->
<?php } ?>
or
<?php
if (is_page( 'Page Title' ) ):
# Do your stuff
endif;
?>
When I view regular page from visual composer, it works fine like this:
http://vrshealth.com/qc2
Margins, backgrounds, etc are all working.
I needed to make a custom post type "quality-check" and am using archive-quality-check.php to display this and the vc-custom-xxxx styles are not loading for some reason:
http://dev-vrshealth.pantheonsite.io/quality-check/
I did some research and the only thing I could find is that page-specific VC styles don't work with Ajax-loaded pages. But it is not loaded through ajax.
Here is the relevant code from archive-quality-check.php which displays if you haven't already chosen a product lot # to display:
<?php if ($_SERVER['REQUEST_METHOD'] != 'POST'): ?>
<div class="col-xs-12 col-md-12" id="page-content">
<?php
$post_id4098 = get_post(4098);
$content = $post_id4098->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
WPBMap::addAllMappedShortcodes();
echo do_shortcode($content);
?>
</div>
I feel like I must be missing something here, like a function to output metadata or some type of custom css, but I can't find any documentation which explains how.
Had the same problem.
Just insert this before echoing content.
get_post_meta( $id, '_wpb_shortcodes_custom_css', true )
Worked for me on latest WP and VC versions.
The answer that Laurent gave worked great for me! However I would suggest creating a function for it in your functions.php file. Maybe something like this:
function vc_custom_css($id) {
$shortcodes_custom_css = get_post_meta( $id, '_wpb_shortcodes_custom_css', true );
if ( ! empty( $shortcodes_custom_css ) ) {
echo '<style type="text/css">';
echo $shortcodes_custom_css;
echo '</style>';
}
}
Then you can just use vc_custom_css($yourPostID); whenever it is required.
Hi I had the same issues, so I have searched in the pluggin, and finaly this works for me :
$vcM = Vc_Manager::getInstance();
$vc = $vcM->vc();
$vc->addShortcodesCustomCss($pop_up_id);
The issue with vc-custom-xxxx styles described in this topic happening to me quite frequently, with various WordPress Themes.
I have tried an approach with function from the second answer. However there is still the issue, when loading page preview via "Preview changes" button. The style' meta data does not updated unless you manually re-save the page before viewing the preview.
The most correct answer was given by Pierre Mar, but I'd like to put a small note on it. You need to replace $pop_up_id with your current post id.
The final function would look like:
function vc_echo_custom_css($post_id) {
$vcM = Vc_Manager::getInstance();
$vc = $vcM->vc();
$vc->addShortcodesCustomCss($post_id);
}
Include it in your Child's theme functions.php file, and then you can call it from any template like:
vc_echo_custom_css(4098);
I am using advanced custom fields in wordpress and having this line of code:
<p class="small"><?php the_field('somefield', 'options'); ?></p>
And it is printed like this in the browser:
<p class="small></p>
<p>the content of somefield</p>
Why does this happen and how do I fix it?
it's because you are using paragraph inside and paragraph you can fix it by using the fix below , if you are inserting the <p> inside this field , please use span or something else it will cause this problem if you use <p>:
<?php
$myfield = the_field('somefield', 'options');
echo strip_tags($myfield, '<p></p>');
?>
#Arsh gave a good answer.
Let me explain the inner-workings of it in WordPress Methodology.
Most function which starts with the_{function_name} most of the time has a function get_the_{function_name}.
Since the_{function_name} is a function to output the field and not threat it has data unlike get_the_{function_name}.
Here is a simple example with the_ID()
function the_ID() {
echo get_the_ID();
}
And here is an example with get_the_ID()
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
Understanding those pattern better your understanding of WordPress conventions and its inner workings.
You can always check official documentation to understand WordPress functions.
https://developer.wordpress.org/
or check in with plugin developers docs.
This is my first question.
I have a custom taxonomy set up in a theme inside a functions.php file.
I've added some extra meta fields for the custom taxonomy (category), also set through the functions.php file.
I am using the update_option() function.
Here is the part that is saving the options to the DB:
<?php
// save extra category extra fields hook
add_action ( 'edited_artists', 'save_extra_category_fileds');
// save extra category extra fields callback function
function save_extra_category_fileds( $term_id ) {
if ( isset( $_POST['Cat_meta'] ) ) {
$t_id = $term_id;
$cat_meta = get_option( "category_$t_id");
$cat_keys = array_keys($_POST['Cat_meta']);
foreach ($cat_keys as $key){
if (isset($_POST['Cat_meta'][$key])){
$cat_meta[$key] = $_POST['Cat_meta'][$key];
}
}
//save the option array
update_option( "category_$t_id", $cat_meta );
}
}
?>
In my template file I am calling them like this:
<?php
$terms = wp_get_post_terms( $post->ID, 'artists');
foreach ($terms as $term){
$term_id = $term->term_id;
$term_name = $term->name;
$term_taxonomy_id = $term->term_taxonomy_id;
$term_slug = $term->slug;
//do you term meta stuff here
//print_r($term);
}
?>
This is where I use them (among other things), and it is of course inside the LOOP:
<div class="single-sculpture-artist-info">
<?php
$category_meta = get_option( "category_$term_taxonomy_id");
?>
<a href="<?php echo get_site_url(); ?>/artists/<?php echo $term_slug; ?>">
<img src="<?php echo $category_meta['artists_photo'] ?>" alt="<?php echo $term_name; ?>">
</a>
<h3>
<?php echo $term_name; ?>
</h3>
<p><?php echo $category_meta['artists_city_province'] ?></p>
<p><?php echo $category_meta['artists_bio_excerpt'] ?></p>
</div>
All of this code works perfectly.
I started adding the content, but then it suddenly started to fail. I think it started when I tried to use one of the category (taxonomy) names I used while developing this whole system (my guess is that it was cached somewhere or something), but then I tried using it with different name, and adding some other which were not there before, and it fails as well. My best guess is that somehow the options table is overloaded with data (limit or something).
Is that even possible? I dont have a lot, 56 working posts in that taxonomy, and 34 categories(taxonomy terms).
I tried my best to get my head around it but couldn`t find what was the problem.
When I insert like 2 or 3 more posts, it starts messing around. So, this:
<a href="<?php echo get_site_url(); ?>/artists/<?php echo $term_slug; ?>">
outputs correct link, but, this:
<img src="<?php echo $category_meta['artists_photo'] ?>"
doesnt. It outputs data from some other category (from the same CPT). I can provide additional info upon request.
Not answering to your question but do you know there’s a big change happening with WP 4.2+ where they’re going to be splitting taxonomy terms, so that taxonomy terms don’t share the same term id if their slugs match.
Please take a look at the links below for some details on how this can be addressed
https://make.wordpress.org/core/2015/02/16/taxonomy-term-splitting-in-4-2-a-developer-guide/
https://developer.wordpress.org/plugins/taxonomy/working-with-split-terms-in-wp-4-2/
Through the help of my network I found the solution to the problem. The line where I am calling:
<?php
$category_meta = get_option( "category_$term_taxonomy_id");
?>
I switched to this:
<?php
$category_meta = get_option( "category_$term_id");
?>
This seems to have resolved the problem. However, here are some other solutions that might be more relevant in the similiar situation:
https://en.bainternet.info/tax-meta-class-faq/
also, be sure to read the things Joe Wilson posted above. Thanks again.
While using the Custom Fields Plugin, I cannot get it to return any data.
I have created a field group called book_cover_thumbnail which has one post linked to it. Can anyone see why the code below would not work?
<img src="<?php get_field('book_cover_thumbnail');?>" />
I get no errors at all, no white space.
Make sure you are a) Echoing the field using either the_field() or echo get_field(), and b) this code is either within the wordpress loop like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>`
<img src="<?php echo get_field('book_cover_thumbnail');?>" />
<?php end while; endif; ?>`
Or you add the post id to the get_field() function as a parameter:
$post_id = *your_post_ID_here*;
<img src="<?php echo get_field('book_cover_thumbnail', $post_id);?>" />
Documentation:
Using get_field(): http://www.advancedcustomfields.com/resources/functions/get_field/
Using the_field(): http://www.advancedcustomfields.com/resources/functions/the_field/
All Advanced Custom Fields Documentation: http://www.advancedcustomfields.com/resources/
Change get_field to the_field. Get field returns the value but doesn't echo it.
Alternatively, put an echo in front of the get field.
Little bit late but also important nevertheless:
You can change the "Return Format" in Custom Fields -> Field Groups -> Return Format
You have the choice between value/label and both(array)
Maybe that could help you in this case