Apply a filter only once

There is no native WordPress hook called add_filter_once(). This is an custom solution which will help you to run your hook only once. For example if inside a loop or whatever situation you are facing. However, The basic idea is to check when you need to stop using your hook and simply remove_hook from WordPress … Read more

pre get posts changing the query

Try using meta_query parameter : add_action(‘pre_get_posts’, ‘add_event_date_criteria’); function add_event_date_criteria(&$query) { // We only want to filter on “public” pages // You can add other selections, like here: // – Only on a term page for our custom taxonomy if (!is_admin() && is_tax(‘event-tag’) || is_tax(‘event-category’)) { $time = time(); $meta = array( array( ‘key’ => ‘start_time’, … Read more

Is there a recover_post hook to go with trash_post hook?

Looking at the code for WP 3.3.2, it seems that trash_post is actually wp_trash_post. From the wp_trash_post() function in /wp-includes/post.php: do_action(‘wp_trash_post’, $post_id); add_post_meta($post_id,’_wp_trash_meta_status’, $post[‘post_status’]); add_post_meta($post_id,’_wp_trash_meta_time’, time()); $post[‘post_status’] = ‘trash’; wp_insert_post($post); wp_trash_post_comments($post_id); do_action(‘trashed_post’, $post_id); So … I’d double check the hook you’re using to set your initial flag. However, there is a hook you can use … Read more

Custom function for “Submit for Review” hook

You need Post Status Transitions actions function notify_me_for_pending( $post ) { $user_info = get_userdata ($post->post_author); $strTo = array (’[email protected]’); $strSubject=”Fstoppers: ” . $user_info->user_nicename . ‘ submitted a post’; $strMessage=””” . $post->post_title . ‘” by ‘ . $user_info->user_nicename . ‘ was submitted a post for review at ‘ . wp_get_shortlink ($post->ID) . ‘&preview=true. Please proof.’; wp_mail( … Read more

How to use the post_updated hook before and after arguments

First off you are using the function wrong. The function is tied to an action within wordpress whenever you use the add_action function. In this case you have added it to the post_updated action. Basically your check_values function gets added to a queue which will then get called by the action when its time to … Read more

do_action and hook methods

Your analogy here is partially correct, but incomplete: I understand that when do_action is called wp looks for all registered listeners and invokes them according set priority. You need to consider what happens when you call add_action. WordPress never searches for the add_action statements, it has those in a list. When you call add_action, it … Read more

action lifecycle

The order and appearance of actions depend on the page and the loaded files. get_search_form() for example fires an action ‘get_search_form’ and (oddly enough) also a filter ‘get_search_form’. But if no search form is loaded the action will not be available. The only way to really know which actions are available is to look at … Read more

Get the post_id of a new post

Try this… add_action(‘post_updated’, ‘myfunction’); function myfunction( $post_id ) { global $post; if (!file_exists(“/www/foo/blog/wp-content/uploads/” . $post_id)) { mkdir(“/www/foo/blog/wp-content/uploads/” . $post_id, 0777); } } NOTE: Change from save_posts to post_updated which will stop the duplicate issue as it fires on “publish” only and not every time you hit add new or update etc. NOTE: I verified this … Read more