ACF Post object not returning data - wordpress

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.

Related

Using cpt and pass a variable using url

I want to create a nice readable permalink structure for my custom post type (CPT). My CPT "movie" has the following rewrite-slug movie/movie_name" (all works fine).
Now i want to add arg like this: movie/movie_name/arg and use arg in my template file as a php variable.
But obvious it lead to not-found-page. How can i achieve this target?
edit: i want it in FRIENDLY URL format, it means i dont want to use GET for this.
You may pass it like movie/movie_name?movie_arg=movie_value. It is will be available with $_GET['movie_arg']. Of course your need extra sanitization to handle this data.
To be able to read this in a WordPress way add params to a query_vars filter
function add_movie_arg_to_query_vars( $qvars ) {
$qvars[] = 'movie_arg';
return $qvars;
}
add_filter( 'query_vars', 'add_movie_arg_to_query_vars' );
Note: it should not be same as reserved WordPress query parameters
This way it will be available at your template with get_query_var('movie_arg')
print_r( get_query_var('movie_arg') ) // movie_value
More information here

How to access WordPress post admin data inside the loop?

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 );

how to show posts data on count hyperlink in wordpress

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.

Change wordpress post author on the fly while publishing the post

I am developing my site and require to change post author id on the fly while publishing.
All post will be write and publish by admin but as content provided by different author and need to change post author id from admin to other author ( author id will get from custom field )
So how to do this on the fly while publishing.
Does the author info have to come from the custom field? Because you could do this straight in the UI manually.
Enable the "author" block in the display options when creating a post
Change the author in the drop-down menu
Here is my theme metabox.php code I am using wpalchemy metabox script and below code is in my cpt loop php file
// getting custom field value from image_artist
// this is giving value like artist_login_name / id
$png_gallery_meta->the_field('image_artist');
$artist_info = $png_gallery_meta->get_the_value();
// to separate artist_login_name and id
$string = $artist_info;
$artist = substr($string, 0, stripos($string, "/") );
// getting first name and last name from user id
$author_first_name = get_userdata(basename($string))->first_name;
$author_last_name = get_userdata(basename($string))->last_name;
So I am using above code to get user info from custom field dropdown selection box. Now how can I use above value to change id on the fly while admin publish the post the post author id should change with above id value get from selection box. I hope now it is more clear.
$post_id = $post->ID; // change this to whathever
$user_id = '4'; // change this too
$the_post = array();
$the_post['ID'] = $post_id;
$the_post['post_author'] = $user_id;
wp_insert_post( $the_post );
The code above will update the currently looped through post (via $post->ID) with an author with ID of 4.
Read how wp_insert_post works here. Basically, if you pass it an ID of a post that already exists it will update that post with the new information you pass to it. (the author ID in this case)
If you want to grab things from the custom fields you can use:
$author = get_post_meta($this->ID, 'author_id', TRUE);
More info on custom fields.
Does the author info have to come from the custom field? Because you could do this straight in the UI manually.
Enable the "author" block in the display options when creating a post
Change the author in the drop-down menu
Here is my theme metabox.php code I am using wpalchemy metabox script and below code is in my cpt loop php file
// getting custom field value from image_artist
// this is giving value like artist_login_name / id
$png_gallery_meta->the_field('image_artist');
$artist_info = $png_gallery_meta->get_the_value();
// to separate artist_login_name and id
$string = $artist_info;
$artist = substr($string, 0, stripos($string, "/") );
// getting first name and last name from user id
$author_first_name = get_userdata(basename($string))->first_name;
$author_last_name = get_userdata(basename($string))->last_name;
So I am using above code to get user info from custom field dropdown selection box. Now how can I use above value to change id on the fly while admin publish the post the post author id should change with above id value get from selection box. I hope now it is more clear.

How can I add custom meta fields in categories?

Does anyone have any idea how to add custom meta fields while making categories and fetch them in the loop in WordPress? I was wondering how to do that without hacking the WordPress core, but if I do – it won't become a hindrance to update WordPress in the future.
A plugin I have found that comes close is Wp-Category-Meta, but it doesn't have the ability to add checkboxes as fields in Edit Categories.
This will be very useful as users can make certain categories "featured", and then the code can use that meta value in the loop to style "featured" categories differently.
The problem:
Wordpress does not have a structure nor method to store "meta" values for taxonomies.
UPDATE 2017: WP 4.4+ has "term meta"!
For working with term metas use these:
update_term_meta()
get_term_meta()
delete_term_meta()
add_term_meta()
The Actions below are still valid though! :)
Additional reading: 4.4 Taxonomy Roundup
Solution for WP version <= 4.3.x and COMMON actions
Actions:
create_category and edit_category for category edit
category_add_form_fields and category_edit_form for category form fields
There are more actions than I've presented, but they seem to be deprecated (according to developer.wordpress.org).
The reason I chose the actions that I chose:
- They work on WordPress 4.4.2
- Due to lack of documentation I assumed these are the new ones replacing the deprecated ones...
Functions:
get_option( $option, $default );
update_option( $option, $new_value, $autoload );
update_option has two great abilities:
a) It craetes the option when such option does not exist yet
Unless you need to specify the optional arguments of add_option(),
update_option() is a useful catch-all for both adding and updating
options.
b) $new_value can be an integer, string, array, or object.
You may ask, why to use array/object? ...well, because each option = 1 database row => you probably want to store your category options in one row :)
The CODE
function my_category_form_fields($tag_object){
//output/display extra form fields, e.g. by echo ...
//ADD EXTRA SPECIFIC FIELD TO LATER CHECK IF IT'S CATEGORY SAVE/EDIT!
//(see note at 'edit_category' action...)
if( !empty($tag_object['term_id']) ){
//edit category form specific
//...load existing options with get_option( $option, $default );
} else {
//create category form specific
}
}
function my_category_save(){
//CHECK FOR YOUR EXTRA SPECIFIC FIELD TO CHECK IF IT'S CATEGORY SAVE/EDIT
//(see note at 'edit_category' action...)
//SECURITY CHECK
if( empty($_POST['EXTRA_SPECIFIC_FIELD']) || ! current_user_can('manage_categories') )
return null;
//save your form values using update_option()
//Recommendation:
//Add "category_" prefix and $category_id to your option name!
}
add_action( 'create_category', 'my_category_save', 10, 1 );
//Runs when a category is updated/edited,
//INCLUDING when a post or blogroll link is added/deleted or its categories are updated
//(which causes the count for the category to update)
add_action( 'edit_category', 'my_category_save', 10, 1 );
add_action( 'category_add_form_fields', 'my_category_form_fields', 10, 1 );
add_action( 'category_edit_form', 'my_category_form_fields', 10, 1 );
Create or Edit?
You might wonder whether you are creating or saving a category - this not documented yet (as far as I know), but from testing:
Edit save => $tag_object is object and contains some properties, most notably:
term_id
taxonomy
filter
Create save => $tag_object is just a regular string "category" - I guess this might change in the future...
General taxonomy
There are also actions like these for taxonomies in general - check these actions.
Jaz,
It looks like the plugin you mention in your original question has been updated to include a checkbox field (included in v1.2.3)
I think the Category SEO Meta Tags plugin will help you.
There is an updated and refactured version of this plugin to be found here:
https://wordpress.org/plugins/custom-taxonomy-category-and-term-fields/
Also added a WYSIWYG editor fieldtype.

Resources