Advanced custom fields - Can't get all the fields - wordpress

Haven't used this in a while and not sure if this is right. I have a few custom fields with the same key. For example:
weapon -> {"name": "Club"}
weapon -> {"name": "Bow"}
Using this example: https://www.advancedcustomfields.com/resources/get_fields/ I'm trying:
echo($post->ID);
$values = get_fields($post->ID);
var_dump($values);
But it returns 24bool(false). The post id is there but no custom fields. I tried with unique keys like weapon1, weapon2 just to test but it doesn't fix my problem.
What I would like to do is get all custom fields for any key and display them in a loop.
Something like $values = get_fields("weapon", $post->ID);
Doing this only returns one value. I currently have two with same key.
How can this be done?

Related

Select posts that have a specific term using withSelect in Gutenberg block

I'm trying to select posts -- or more specifically, a custom post type -- that belong to a specific term, and display them in my Gutenberg block's edit screen. I'm wanting to recreate what would be tax_query with WP_Query, but in Javascript.
I'm able to select posts, but I'm unsure what parameters to use with getEntityRecords to select by term (or if it is even possible). The documentation is still a little vague at this point.
Here's where I'm at. This successfully selects all posts of the 'rmenu' type:
const items = select("core").getEntityRecords(
"postType",
"rmenu"
);
Does anyone know if getEntityRecords is the right way to handle this?
Thanks.
The third parameter to getEntityRecords is a query object. You can pass any query argument accepted by Wordpress API such as categories or tags. Selecting by category term would look like this:
const items = select("core").getEntityRecords(
"postType",
"rmenu",
{ categories: [ 13 ] }
}
Although Capi's answer is correct, it wasn't clear for me that this would also work for custom taxonomies. It turns out wp.data will add your custom taxonomies automatically as properties to the post object. For example, a post could look like this:
{
title: "hello world",
content: "this is a post",
id: 123,
type: 'my-custom-posttype'
my-custom-tax: [5, 8, 24],
...
}
So, in order to get all the posts of the type my-custom-posttype that have a term with ID 4 or 8 of the taxonomy my-custom-tax, you would run this query:
wp.data.select( 'core' ).getEntityRecords( 'postType', 'my-custom-posttype', { 'my-custom-tax':[4,8] })
Important! If you test this in your browser, you need to run it twice. The first time it will return an empty array, because it only invokes the promise.

ACF Post object not returning data

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.

Call setter method with variable name

What I'm asking here is something weird. I'm using Symfony2 with Doctrine2. To bypass a issue between FOSRestBundle and JMSBundle (who don't like composite primary key) I need to know if it's possible - and how - to call a setter from the name of the field.
Here an example: imagine my Product entity have a primary key composed by 3 fields.
The trick is to send an array this kind of JSON into my API REST:
{
"id": {
"field1": "xxx",
"field2": "...",
"field3": "..."
},
// other fields
}
Then I use json_decode() to extract the ID field. I create a table of $key => $value with the 3 primary keys. And there is my problem, with this table I want, in a foreach loop, to do something like $myEntity->set$KEY($VALUE).
I'm doing this because I want to reuse this code with all my entities.
I know that this is something really weird, to sum up with only the name of the object field/property I want to call is appropriate setter().
Thanks ;-)
PS: sorry if my English isn't perfect, this isn't my birth langage.
You can use dynamic method name in PHP :
$myEntity->{'set'.$KEY}($VALUE);
or more readable version :
$method = 'set'.$KEY;
$myEntity->$method($VALUE);
Don't forget to check if method exists anyway :
$method = 'set'.$KEY;
if (!method_exists($myEntity, $method))
throw new \Exception('Something bad');
$myEntity->$method($VALUE);

Show up Woocommerce custom fileds into the CSV export

I'm using "Woocommerce CSV Export" plugin and i've just added 2 custom fileds into my WC checkout page, I was wondering if i can add those fileds to the export plugin.
Just add the following code into your "woocommerce-export-csv.php"
add_filter( 'woocommerce_export_csv_extra_columns', 'sites_add_custom_meta', 10);
and the define your function
function sites_add_custom_meta() {
$custom_meta_fields['columns'][] = 'IP Address';
$custom_meta_fields['data'][] = '_customer_ip_address';
return $custom_meta_fields;
}
In this function you have two things you need to define. First, in the columns array you need to define the titles of your custom meta fields and then in the data array you need to define actual meta field name where the data for that field is located at. You need to have the same order in both arrays to be able to have correct information output under right column title.
Source : http://docs.woothemes.com/document/ordercustomer-csv-exporter/#section-4

drupal 7: how to access list-field's key instead of its label (field api)

I'm working on a custom theme and googled the whole day but can't find an answer to my question:
how to print out a cck list field's key instead of its label?
I think it's the right way to access fields via the field api, right?
so I try
$output = field_view_field('node', $node, 'field_list');
print render($output);
that seems to be the way to get the label value of the key|label pair. but even if i set the display options format to 'key' - it only prints the label value. what am I doing wrong?
I know there are other options to render the key but how is it possible using field api?
You can get the field value itself (which will be the allowed values array key) with field_get_items():
$items = field_get_items('node', $node, 'field_list');
$key = $items[0]['value'];
If you need to match those up again at any point you can get the full key/value list from the field metadata:
$info = field_info_field('field_list');
$values = $info['settings']['allowed_values'];
$label = $values[$key];
Here's a similar way. This example works for a Country list with ISO codes.
The idea is to render the Name of the Country that was selected because the dump returns only the key ( iso code in this case)
Format of the select list on the backend:
AR|Argentina
US|United States
Assuming you have selected United States on the backend and you want to print the country name on the node template:
$country_field = field_info_field('field_country_iso');
$country_iso = $node->field_country_iso[LANGUAGE_NONE][0]['value'];
$country_name = $country_field['settings']['allowed_values'][$country_iso];
then
print $country_name; // This will return United States, not US code
I hope this helps.

Resources