Adding tag and custom post meta - php

The above function then checks for a condition for two scenarios:
1. Image is selected: a post is created from the first function when an image is selected. Because it already has the post_id, it simply updates the "dummy" content with the real content from the input fields
2. Image is not selected: as an image is not selected, then there is no post created from the first function. Because there is no post (thus no post_id), it creates a new post using the input fields.
Problem:
There are total 4 input fields that I need to have: title (title), description(content), tag(rh_tag) and custom field (custom_1).
I am having a problem with tag and custom field in a scenario when an image is selected (the function is not completed).
So, let say, if I do not select an image (thus no post is created from the first function), then the second function (and "else" part) simply creates a new post with all the four input. This works just fine.
However, if I select an image, then I am missing something and I can't somehow make the function work. I tried to add same $post =array as the second function, but it was not successful.
Can someone help me out how to "complete" the first function so that I can add tag and custom post meta even when an image is selected?

At first, look at WordPress codex. As I see, you have incorrect usage of update_post_meta. You should use post_id as first argument of the function, not array
At second, before accessing value from $_POST array, be sure that it exists:
$customValue = isset($_POST['custom_1']) ? $_POST['custom_1'] : 'default value';

Related

Delete ACF user profile repeater field row from user account (WordPress)

I am using ACF repeater field for user profiles, which shows like this in the user profile page:
That's cool.
But on the front-end I have a form that I want to use to delete a specific row. My custom form simply lists all the rows with radio buttons, and so if the user selected number 3 and submitted the form then I want to delete the third row (in this example, the row with Catherine Davies would be deleted).
The form works fine in that it submits as expected and returns the value the user selected, but my code to delete the row that was selected doesn't seem to work.
The ACF documentation seems a little vague on the subject. Based on a combination of the ACF doc and this StackOverflow post, I expect this code to work but it does not:
$user_ID = get_current_user_id();
$field = 'extra_user_info'; // Name of the repeater field
$row_to_delete = $_POST["row_to_delete"];
delete_sub_row($field, $row_to_delete, 'user_' . $user_id);
Just to be sure, even if I hardcode the $row_to_delete variable to any number (from 1 to 4) it still does not delete any row.
Just to clarify, I wish to delete an entire row.
Note: I realise I could just embed the ACF form on the front-end, but for reasons I won't go into this is not an option, hence using my own custom form.
There could be another issue but in your example at least, your $user_ID variable has capital letters, while the variable passed to delete_sub_row() is lowercase. PHP variables are case-sensitive.

How can I modify the value of an Advanced Custom Fields Post Object field via PHP?

I'm working on creating an import script to migrate data from an Excel spreadsheet that a client is providing, and converting each row into a WordPress post. So far, I've got the posts being created and all custom fields being filled in properly...except for one.
One of the fields is Associated Parts. I'd like to use the Post Object for this field, but I can't seem to find any documentation on the formatting to use in order to assign a post object (or preferably multiple objects) to this field's value via my PHP script.
The following is an example of some code that I'm using to populate the "specifications" repeater field that has two sub fields, label and value.
$field_key = 'field_53ef95cead820';
$value = get_field($field_key, $postID);
foreach($specs as $spec):
$specArray = explode(':',$spec);
if($specArray[0] && $specArray[1]):
$value[] = array("label" => $specArray[0], "value" => $specArray[1]);
endif;
++$i;
endforeach;
update_field( $field_key, $value, $postID );
To modify the associated parts field, should I set it up as a repeater like this and then create some sort of array to populate it, or should I use the multi select option and still pass some sort of an array to it. I'm happy to go either route, I just need some way to get those fields in there.
It's impossible to add associate products (other posts) during the initial import due to the fact that while you are importing the first product into WordPress, it's associated parts have not yet been created. For this reason I had to run two imports of the spreadsheet into WordPress.
On the first import, it created all the posts and added all of the non-relational custom fields as they appeared in the spreadsheet. I setup the script the print the post ID's of the imported products in a table format that allowed me to easily copy and paste all of the ID's at once from the script output back into a "Product ID" field in the spreadsheet. The script checks for the existence of a product ID and this field is what will determine if it is creating a new post/product or updating an existing one.
Once all the products had been imported and their respect ID's were added to the spreadsheet, we began the second import. We used the exact same spreadsheet with the only difference being that now it had the WordPress ID's added to a column.
1. if($product['Associated Parts']):
2. $assParts = explode('|',$product['Associated Parts']);
3. unset($assIDs);
4. foreach($assParts as $assPart):
5. unset($assID);
6. if($assPart):
7. $assID = get_page_by_title( $assPart , 'OBJECT' , 'product' );
8. $assIDs[] = $assID->ID;
9. endif;
10. endforeach;
11. $assIDs = array_filter($assIDs);
12. update_field( 'field_542c5dc44272e' , $assIDs , $product['Page ID'] );
13. endif;
Breakdown of the code line by line:
I check to see if this product has any associated parts assigned to it.
I convert the pipe separated list of associated parts into an array.
I unset the array that we will be using to store the array of post ID's. I do this so that the associated parts from the first product are not still stored in the array when we begin processing the second product.
I begin looping through the array of associated parts. Each item in this array contains the title of the associated part. In order for this to work, the titles must be spelled, formatted and in all other ways identical to the title of the WordPress post.
Same as 3, only now it's for this particular associated part.
This line was added to ensure that I wasn't searching for an associated part if that item of the array was an empty string.
I retrieve the WordPress post item that matches the title of this particular associated item.
I add the ID of the associated product to a numeric array.
Close the if statement on line 6.
End the associated products loop.
Filter out any empty strings or null values from our array of post ID's.
We pass the the numeric array containing the post ID's into the Advanced Custom Fields "update_field()" function. The first parameter is the unique field key of the Post Object custom field. The second parameter is the array of associated products ID's. The third parameter is the ID of the product we are editing.
Close the if statement on line 1.
I had to guess as to the format I needed to use for the value of the field. At first, I tried passing in an array of Post Objects and that didn't work. In the WordPress post editor, I inspected the code and saw that the value of their input field (when another post was selected) was that posts ID. After switching to a numeric array containing the Posts' ID's, the update_field() function accepted them perfectly without incident.
We're done.

Magento selected Attribute result

I would like to add a Text depends on selected Attribute option: I create a Attribute with the optiones YES or NO.
I want to create with php if I select in the Admin Panel yes that if should start another php code (that will show a few text on the category pages)
But if I select No it should not run through the code, I mean don't display the text
If you created a Yes/No attribute (select Yes/No for the Catalog Input Type field), then you can check the value simply using
$_product->getData('my_attribute_code');
This will return either 0 or 1.
So if you try
if($_product->getData('my_attribute_code')){
echo 'My text';
}
Note that depending on the context you may have to load the product first to get this value, so if nothing comes out of the getAttribute function, so you may have to execute this code before the previous one :
$_product->load($_product->getId())
And this should work.
EDIT: Note that this will not work if you create a dropdown attribute and set manually Yes and No as values. You need to chose Yes/No for the Catalog Input Type field of your attribute.
Cheers

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.

Drupal views filtering content by node id

I have the following scenario:
I have a content type called 'Product' in which there are the default input fields and a number of custom input fields that have been created using CCK.
When a new product is created, all of the data entered is posted as a node (as normal).
Part of the problem is that I don't want the values from the custom input fields to be within the node content. Therefore, my thinking was to hide the display of these fields. I could then create a block of the custom input fields using views.
Is it possible within views to set up some filtering that will only display the values of the custom input fields on the node from which it was originally created? For example if the product I created had a node id of 1, is it possible to filter the view to display the values of the custom input fields that were created from node id of 1 and then display these values as a block on node id 1?
This might sound a confusing approach but I am keen to separate some of the data entered into the Product content type from the main node content, mainly for styling reasons.
Thanks, Mark.
Ah worked it out. It's always an easy solution isn't it?
In the Arguments section of the view, add Node: Node ID. Select the action of 'Provide default argument' and then select 'Node ID from URL' from the 'Default argument type:'.
This will then just display the values of the custom input fields that was created on a particular node.

Categories