Trying to populate custom fields in Wordpress from WP database - php

I'm trying to populate a custom field, let's say 'field_123' (in this case from ACF plugin) with data from a Wordpress database table (which is coming from an API), let's say 'house' > 'square_metres'.
Posts (cpt) are already showing the main content 'out of the box' but I would want to add some more database tables to populate other custom fields. (which I then can use later on on different grids in visual composer)
I started with something but then realized soon enough I wasn't gonna solve the problem myself..
global $wpdb;
$new_value = $_POST['acf']['field_123'];
$wpdb->update(
"house",
array(
"square_metres" => $new_value,
),
array( '%s' )
);
}
add_action('acf/save_post', 'update_field', 1);```
Someone any suggestions? Or a push in the right direction? Perhaps there are smarter/easier ways to achieve this.
Thanks!

Is it all on the same DB? "house" is an existing table? what's it's name on the database? are you sure you didn't call it "wp_house"? in that case you are missing $wpdb->prefix."house" as the table name.
That last row of "add_action" is confusing me, what were you exactly trying to do? With that code, if the "house" table exist, you whould be able to populate its square_metres value with what you have in $new_value just fine (be careful, you didn't specify any WHERE clause, you are going to update ALL the rows in a single shot, dunno if this was intentional).
Is your post missing a piece of code?

Related

Get Child Table Values linked to Specific Parent Table Value (dependent dropdown)

In my Blade file, there are two dropdowns, "Countries" and "Regions". I want the Regions dropdown only to display the values linked to the country selected in the "Countries" dropdown.
For that, I believe, I have to pass a variable for $countries and $regions in order to compact them and call the variables in the blade file.
This is what I currently have (which is totally wrong), but what should it be?
public function create()
{
$countries = Countries::orderBy('name')->get();
$regions = Regions::where('countries_id' == $countries('id'))->orderBy('name')->get();
return view('admin.cities.create', compact('regions', 'countries'));
}
I know that the $regions variable part "where('countries_id' == $countries('id'))" is incorrect, but I've tried multiple other ways, and still not able to figure it out.
Is this variable = model::where(foreign_key == parent_table ID) possible?
All my relationships have been set up with Laravel Eloquent already.
Any assistance would be appreciated
I started to write how to fetch data from controller(Country::all and Regions::all), and what to do in blade file and ajax functions that does stuff on events and figured - maybe there already is a solution.
Sorry, I don't have 50 rep to comment, but if you follow what they did here (both answers), I think you're gonna figure out what to do in your example :)
Keep in mind you need the part in the bottom of the page!
Laravel dynamic dropdown country and state

Advanced Custom Field checkbox query not working

I am using an Advanced Custom Field to allocate a custom field to posts.
This field is then used by a filtering system to filter posts using a custom query (via the pre_get_posts action). The other query arguments are working fine, except for the one which relies on ACF’s field.
The current query arguments of the field’s meta_query have been constructed according to the ACF documentation for the field (hence the quotation marks surrounding the value).
Can anyone advise me as to where this is going wrong? The query doesn’t seem to return any posts based on the values I am passing to the query.
Query arguments:
$courses = get_query_var('courses');
if ($courses) {
$query->set('meta_query', array(
array(
'key' => 'course_check',
'value' => '"'.$courses.'"', //i.e mdia-403
'compare' => 'LIKE'
),
)
);
}
ACF stores checkbox values in array, that's the reason your code is not working.
See working example here:
https://support.advancedcustomfields.com/forums/topic/wp_query-using-meta_query-for-an-acf-checkbox-field/
Found the cause of this issue by print_r’ing the entire query and inspecting what was actually going on - had done this a number of times, but overlooked the following detail:
The issue here wasn’t actually due to the checkbox meta_query, but a conflict between the get_query_var term (‘courses’) and a custom taxonomy whose slug is also ‘courses’.
When the query was executed, the query seemed to be querying for posts associated with both the custom taxonomy and the selected checkbox value, yielding no results.
Renaming the name attr to something other than courses then reconfiguring the registered query var and meta_query fixed this issue.

Magento $collection->join - Multiple

I'm currently extending the Magento Invoice Grid to display some additional columns.
I've got the data that I need for most, but I'm struggling to get more than 1 Column from the same Database Table.
This is what I have in my _prepareCollection
$collection->join('invoice','main_table.entity_id=invoice.entity_id','subtotal');
$collection->join('invoice','main_table.entity_id=invoice.entity_id','discount_description');
I assume I need to drop the second line and put them into somekind of array, I'm just not sure on how to do that as I don't work with PHP too much.
Thanks.
Oh, this was much more simple than I was expecting, I managed to work it out with the following:
$collection->join('invoice', 'main_table.entity_id = invoice.entity_id', array(
'subtotal',
'discount_description'
));

Wordpress Gravity Forms Serialized List

I've got a List field with Gravity Forms to populate some custom fields in a custom post type. The site is a recipe submission site, and I'm looking for users to be able to add ingredients individually for better SEO. My issue is, when I submit the form only the last input field under ingredients is passed to the recipe.
I know I need a serialized list as this custom field pulls an array, but I'm at a complete loss of how to do that. The array should read something like this
a:8:{i:0;s:26:"4oz piece of salmon/person";i:1;s:12:"1 egg/person";i:2;s:37:"1-2 multi-colored bell peppers/person";i:3;s:12:"Greek olives";i:4;s:9:"Olive oil";i:5;s:13:"Salt & Pepper";i:6;s:22:"Basil (fresh or dried)";i:7;s:0:"";}
I don't even know where to begin in putting together a serialized array for one form field, so any nudge in the right direction is greatly appreciated.
Unfortunately Gravity Forms is configured to store these as separate meta records. One option is to customize the Gravity Forms forms_model.php file, create_post function, which unserializes the field contents and loops through each item to create a new post_meta record.
The following code should replace the case for field type list, and will prevent the creation of individual meta records on a predefined array of Gravity Form fields:
case "list" :
$skipped_list_fields = array('<meta name for field to skip unserializing>',
'<meta name for another field to skip unserializing>');
$value = maybe_unserialize($value);
if (in_array($meta_name, $skipped_list_fields)) {
if(!rgblank($value))
add_post_meta($post_id, $meta_name, $value);
} else {
if(is_array($value)){
foreach($value as $item){
if(is_array($item))
$item = implode("|", $item);
if(!rgblank($item))
add_post_meta($post_id, $meta_name, $item);
}
}
}
break;
Jason, someone had a similar situation https://stackoverflow.com/questions/20591802/how-to-save-comma-separated-inputs-in-gravity-forms-to-a-global-array-on-form-su - I think a similar edit in functions.php in your theme is the direction...
With a Custom Field form field configured as a List type and having filled out the form like this, I'm seeing all the ingredients as separate post meta items (but all attached to the same key) like this.
Is this different from what you are seeing or are you trying to achieve something different?
Use the 'List' field type from the 'Advanced Fields' area when creating/editing your form. That data is already serialized and saved as an array in a single field. Just use the gform_after_submission hook to save the GF field as postmeta.

GridView and filtering using a lookup table

I'm using a lookup table as described in the blog tutorial. So one of my grid columns looks like this:
array(
'name'=>'status',
'value'=>'Lookup::item("PostStatus",$data->status)',
),
If I type "Published" in the Gii generated CRUD filtering system I get nothing. If I type the status code, like 1, I get the correct results.
How can I fix this? I mean I need to be able to type "Published" or "Draft" instead of 1 and 2.
You should provide more information like #Sukumar suggested, however I have kind of a supposition: if status is a number and you are directly writing down that number, I think something like this could be happening:
You write the desired status code
Somehow you pass that number to $data->status
Lookup searches an item with that status as status (just like the id)
Yii gets that number and returns a PostStatus given by that id.
I'm not used to that Lookup::item function, but you should find the way to pass inside the function the statusname (I don't know how you call its name).
I'm sorry, but that's all I can find with that amount of information. I hope it helps

Categories