in_category relies on a global post – it should only be used within the loop. Instead, use the arguments that are passed to your callback to query the categories assigned to the post being edited and check if one of them is video:
$cats = get_the_category( $post['ID'] );
if ( in_array( 'video', wp_list_pluck( $cats, 'slug' ) ) {
// In "video" category
}
…altogether now:
function attachment_field_url_save( $post, $attachment ) {
if ( isset( $attachment['video-url'] ) ) {
$cats = get_the_category( $post['ID'] );
if ( in_array( 'video', wp_list_pluck( $cats, 'slug' ) ) {
update_post_meta( $post['ID'], 'video_url', esc_url( $attachment['video-url'] ) );
}
}
return $post;
}
Related Posts:
- How to add a class to the attachment images
- How to automatically apply woocommerce product title to all product images alt tags?
- exclude multiple terms using get_terms() function
- Counting number of posts in a category and its sub categories and displaying result using shortcode
- Allow authors to post only in categories they create in WordPress
- How to Populate the list of custom post type categories
- Php string not working in WordPress Functions.php [duplicate]
- How can update custom meta for all posts
- Problems with function on function.php
- Use template for posts with a particular category grandparent
- Check if Product is in a Specific Category in Functions.php
- page template for attachement page?
- Woocommerce – remove product from category
- get current product name in functions.php
- get post based on category chosen in drop down – The ajax method
- Swapping wp_dropdown_categories function with wp_category_checklist
- Passing the custom field values in the wp_get_current_user array function
- Pass Category Name, Description and Photo into variables to pass to jQuery
- How to pick the default selected value in wordpress dropdown?
- Firing schema via code in functions.php doesn’t work
- Get page that displays all children of taxonomy parent
- 3 Slashes appear after Apostrophe in custom fields after updating product-site
- Trying to retrieve post meta
- Redirect to another page using contact form 7? [closed]
- Dilemma of Populating all the categories in a drop down list
- Which PHP page is the Default Posts Page
- Edit the_category (); for a hierarchical display
- How to check if a PHP string is different than meta field?
- Can’t save php string to a custom field
- Why does this update_post_meta function not delete the custom field itself?
- How to show/hide php table rows based on the content of custom fields
- Displaying posts based on category
- Custom meta box values are not getting saved for my custom post type
- Show ACF field with link to ultimate member profile/WordPress user profile below the post (single post layout)
- How to get current post category details inside “loop”?
- WP All Import / Update stock quantity from multiple XML files
- Changing the order of custom fields in the dashboard for Woocommerce variable products [closed]
- Show all tags within a category?
- Post not populating for custom post type based on category selection
- How to add data to a custom field at the wp_users table?
- Problem with adding custom CSS class to image in ACF Photo Gallery plugin [closed]
- Define category ID using get_post_meta
- Hide a div when a custom field is empty
- Let users upload image(s) to the post from front end
- Conditional On custom field plugin metabox
- Delete images from media library when user deletes an image from ACF Gallery
- Updating Metadata with Shortcode
- Order category posts by last word of custom field
- Add a “custom field” to a category that can be retrieved when viewing the category page with get_post_meta
- Dropdown category field inside repeatable metabox
- Products listing check if meta checkbox is checked
- Trying to update Woocommerce meta values
- Display Visual Composer shortcode if a post belongs in specific categories
- Is it possible to update the dataset using update_post_meta
- Block error message in foreach loop when looping through ACF field
- Hide a div if the fiels is empty
- Get parent category name when only child category is applied to a product
- How to move a post from one category to another
- Notice: Undefined variable
- Hide a specific category title from displaying on site
- How do I get the value of a current user’s custom field?
- Store custom field’s multiple values in one user meta key
- Get field in readable word
- Populate Posts based on category selected using AJAX that means without page refresh
- All categories options or All categories not Populating
- Customizing the output of the archive and category widget without altering the original behavior of the widget
- PHP get_category() function redeclared
- Get category id for a custom category and display it in a class
- Trying to see if page is category or single and displaying title with appropriate heading tag
- Display custom meta box in my template file
- ACF Date fileds to Age Convert [closed]
- Displaying categories
- Appending an ACF custom field to the page title
- How to insert the first letter in uppercase [closed]
- Hide child of parent categories already hidden with “get_terms_args” – Woocommerce
- How to pass value to add_filter wpcf7_form_tag from another function?
- Custom Admin Menu Report for Specific User ID
- Exclude a category ID from the following function
- ACF number less than comparison not working
- Exclude function for custom pages, exclude custom ACF fields
- Error Warning: Declaration of description_walker::start_el after PHP upgrade [duplicate]
- Autoloading in Child Theme
- Add a class to the anchor tag on HTML5 gallery
- Display a specific category of products in shop page and disable code for specific actions
- Saving and Restoring a Canvas on A Individual User Basis
- wp_force_remove_style’ not found
- Pagination on Custom Post
- how to save checkbox data for custom setting?
- How to add aria role and schema markup to custom walker container
- Adding custom url to readmore link using get_permalink()
- Remove submenu item from list
- How to include a function in a template with template tag
- wordpress all post filter by year
- Pulling a variable into the wp_nav_menu function
- If custom field doesn’t exist, or exists and it’s true show title?
- Custom fields randomly stop working
- Problem with function.php.. like
- Frontend redirect after delete post in wordpress
- Displaying custom meta box value in a custom post page
- Link on post title only if post have content