wordpress advanced custom fields select values - wordpress

I'm working with Wordpress 3.5.2 and plugin Advanced CUstom Fields 4.1.8
I have a group of fields (called "P") and some fields.
I want to retreive all data from a specific select field, and I found some code in the plugin documentation:
$values = get_field('field_519a0279bc93e');
if($values)
{
foreach($values as $value)
{
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}
In $values, getting two different select fields In a case I get a string and in another a boolean.
I supposed is so simple, but I can't find the solution.
Thanks in advance.

It is important to understand that when using the Advanced Custom Fields plugin all this does is create an easier method for admins to add custom fields for posts, so the WordPress get_post_meta() still applies.
Say you've created a custom field called 'sex' and you've assigned a dropdown or radio buttons to it and you wanted to display what this value in the post or the loop, you would do the following.
Outside the loop
## Returns sex custom field value for current page ID
$sex = get_post_meta( get_the_ID, 'sex', true);
Inside the loop
## Returns sex custom field value for current loop iteration
global $post;
$sex = get_post_meta( $post->ID, 'sex', true);

Related

Run function after any category is deleted

I need to delete some of the values of custom field whenever any categories is deleted. I tried this hook but it is not working.
add_action( 'delete_term_taxonomy', function($tt_id) {
// Whatever i need to do after a category is deleted
}, 9, 1);
Can any one help me with this?
EDITED:
I am dynamically populating list of categories into advanced custom field repeater which has fields like category_name, category_description, category_slug, enabled and order.
function my_acf_set_repeater( $value, $post_id, $field ){
// echo '<pre>'; print_r($value); echo '</pre>';
// this one should consists array of the names
$settings_values = get_categories();
$i = 0;
foreach( $settings_values as $settings_value ){
// echo $settings_value->name;
$value[$i]['field_6357b02a9bb1b'] = $settings_value->name;
$value[$i]['field_63594327553ef'] = $settings_value->description;
$value[$i]['field_635945b6aa743'] = $settings_value->slug;
$i++;
}
// echo '<pre>'; print_r($value); echo '</pre>';
return $value;
}
add_filter('acf/load_value/name=categories_to_display_in_homepage', 'my_acf_set_repeater', 10, 3);
This repeater is shown in theme option page, so that user can enable which categories to show in home page along with ordering it.
Fields like category_name, category_description, category_slug are dynamic. Fields enabled and order are custom which user fills in.
Its working fine. But the problem here is, whenever a category is deleted, it is removed from theme option page as well. But the enabled field and order field values are shifted to another category in that repeater field.
So I need to remove that field values related to deleted category as well.

Woocommerce Product Vendor extension - Loading ACF fields into a vendor's store front

I am using ACF to add fields to my vendors' dashboard profile pages. I currently have a test ACF field loading the field from only the WP Admin profile page on all the vendors' product listing page using this simple hook in my child theme's functions.php:
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() { ?>
<?php if(get_field('founded_on')) { ?>
<?php the_field('founded_on'); ?>
<?php }
}
Perhaps I'm pulling the wrong hook, but I can't seem to find the right hook in the Product Vendor frontend page.
I need help customizing it so that when you are on a vendor's product page, it pulls the ACF fields from that particular vendor's profile. Currently, it partially works; however it only pulls the WP main admin's data for all the different vendors'.
I know there is a way to pull the vendor's ID for each particular vendor's page and have it load their data for their page, but my php knowledge is very limited. I usually just hunt for existing code and tweak it. Unfortunately I haven't found any solutions that have worked, and this is the closest I've come to getting custom fields to work on a vendor's page.
Or if anyone can point me to a better solution to allow me to create customer fields for a vendor to fill out that will be loaded on their front end page, that would be great. I've tried Nicola Mustone's solution ( here ), which would have been perfect, except I couldn't get it to load the new custom fields on the vendor's store profile form page, nor have it load the fields into that vendor's storefront page. Based on comments, it only shows up for the site's Admin and only they can edit it. There's no visible way to have it load on the storefront, which defeats the purpose.
I imagine that the providers are users with a certain level within the WordPress system?
Considering that this is your case, the ACF fields need some additional parameters to become visible:
$post_id = false; // current post
$post_id = 1; // post ID = 1
$post_id = "user_2"; // user ID = 2
$post_id = "category_3"; // category term ID = 3
$post_id = "event_4"; // event (custom taxonomy) term ID = 4
$post_id = "option"; // options page
$post_id = "options"; // same as above
$value1 = the_field( 'my_field', $post_id );
$value2 = get_field( 'my_field', $post_id );
take some examples found in the ACF documentation, but in your particular case you have to pass the user's ID
the_field('founded_on', 'user_' . $user->ID );
echo get_field('founded_on', 'user_' . $user->ID );
function documentation the_field()
You need to pull the user ID of the user and then use the format user_{$user->ID} for the post ID as the second parameter of the ACF field.
If I understand your question, then this should work.
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() {
$user = wp_get_current_user();
if ( get_field( 'founded_on', 'user_' . $user->ID ) ) {
the_field( 'founded_on', 'user_' . $user->ID );
}
}

Get post id inside shortcode used in loop

I feel like I'm missing something here. So I'm using visual composer masonry grid to show posts. I'm trying to create a shortcode to use in the Visual composer grid builder that shows the date timestamp but even though the shortcode is being used in a loop I can't get the post id or things like the_title or the_date in the shortcode. I can even use other shortcodes within this one to pull in the title and other meta info and it shows post specific info.
This is my attempt... it outputs nothing, no errors at least, but no result...
function lmi_features_time_ago_shortcode( $atts, $post ) {
global $post;
$output = '';
$timestamp = get_the_date('Y-m-d g:i:s', $post->id);
// $timestamp = time_elapsed_string($timestamp);
$output .= $timestamp;
return $output;
}
add_shortcode( 'social_feed_ago', 'lmi_features_time_ago_shortcode' );
Do a var_dump($post) and check if it's actually the post object. If the loop is set to return ids only then the $post variable will be the ID itself.

Woocommerce /Display Custom Field in Wordpress

I added some custom fields to the each product in Woocommerce, and I would like the data from the custom fields to show on the following receipt page (Order Details) after checkout is completed.
Just can't get it, not strong in php, why can't get anything. I've tried to use var_dump(get_post_custom($order->id)); i don't have my custom field in result.
Can somebody light me up?
Here is my code:
add_action( 'woocommerce_order_details_after_order_table', 'code_activation', 10, 1);
function code_activation($order){
echo '<p><strong>'.__('Activation code').':</strong> ' . get_post_meta( $order->id, 'activation_code', true ). '</p>';
}
You have added custom fields to the product. So custom fields will be associated with your product-id .
But you are trying to retrieve the custom fields using $order->id which is wrong. following code should help to retrieve the product id from order. And using the product id you can retrieve your custom field.
$orderItems = $order->get_items();
foreach($orderItems as $orderItem)
{
$product_id = $orderItem['variation_id'] ? $orderItem['variation_id'] : $orderItem['product_id'] );
}

How to Filter by Custom Field in Fishpig?

There is a way to get all the checked checkboxes of a custom field in Magento/Wordpress(with Fishpig extension):
$post->getCustomField($customfield)
I'm trying to filter the posts by selected checkboxes, and I'm considering to compare the filters to the posts via for loop, but is there a more efficient way of filtering posts?
You can create a custom collection of posts that are filtered by a custom field:
$posts = Mage::getResourceModel('wordpress/post_collection')
->addIsViewableFilter()
->addMetaFieldToFilter('custom_field_name', 'custom field value')
->load();
This will return all published posts that have the value 'custom field value' for the custom field called 'custom_field_name'.
Once you have a post model, the correct way to retrieve a custom field value is with the following:
$customFieldValue = $posts->getMetaValue('custom_field_name');
$posts = Mage::getResourceModel('wordpress/post_collection')
->addIsViewableFilter()
->load();
foreach($posts as $post) {
echo $post->getId() . '<br/>';
echo $post->getPostTitle() . '<br/>';
echo $post->getMetaValue('your_custom_field_name') . '<br/><br/>';
}
This code gets all posts and displays the post ID, post title and the value for the custom field that has the bame of 'your_custom_field_name'

Resources