Hook that get’s triggered when the author of a post is changed

There is no special hook to author change. But you can achieve it by using post_updated hook. Example: add_action(‘post_updated’, ‘prefix_on_update_author’, 10, 3); function prefix_on_update_author($post_ID, $post_after, $post_before) { if ($post_after->post_author != $post_before->post_author) { // author has been changed // you can add your own hook here or write your code } } Here is the codex … Read more

Fetch All Posts (Including Those Using a Custom Post Type) With WordPress API

I ended up extending the API like suggested in the comments to my question, although I was hoping there was a default route that fetched all posts of all post types. Apparently, there isn’t. So here’s my solution: add_action( ‘rest_api_init’, ‘custom_api_get_all_posts’ ); function custom_api_get_all_posts() { register_rest_route( ‘custom/v1’, ‘/all-posts’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘custom_api_get_all_posts_callback’ … Read more

Correct Post Count ( All | Published | Drafts | Pending | Trash ) for Custom Post Type when restricting to view own posts

You have to change: ‘post_type’ => ‘post’, To: ‘post_type’ => ‘your_custom_post_type_name’, And a all the reference to standard post. Also, you should add the filters only for main query or you can end up with issues in secondary queries. Bellow an example code for gallery custom post type. I was trying to debug the code … Read more

SQL query to extract only the “current” wp_posts?

Rather than constructing query from scratch, it is easier to see what exactly is WordPress querying when API function is used: get_posts(array( ‘numberposts’ => -1, )); var_dump( $wpdb->last_query ); Gives following SQL: SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type=”post” AND (wp_posts.post_status=”publish”) ORDER BY wp_posts.post_date DESC

How set featured posts using checkbox in post edit screen?

You can do this by following these steps: Add a custom meta box to your posts Create a function to save the meta data using the save_post action Add the ‘meta_key’ query argument to whatever query you are using. Place this in your themes functions.php file: function register_post_assets(){ add_meta_box(‘featured-post’, __(‘Featured Post’), ‘add_featured_meta_box’, ‘post’, ‘advanced’, ‘high’); … Read more

Get current post id in functions.php

If you hook your localize script function to wp_enqueue_scripts, then you will have access to the global $post variable. As long as you pick a hook at or after ‘wp’ you should have access to the global $post. <?php add_action(‘wp_enqueue_scripts’, ‘YOUR_NAME_scripts’); function YOUR_NAME_scripts() { wp_enqueue_script(‘YOUR_NAME-js’); global $post; $params = array( ‘site_url’ => site_url(), ‘admin_ajax_url’ => … Read more

Adding additional data to WP_Post object

If your extra data directly references a post meta you don’t have to do anything, because WP_Post implements the »magic« methods __isset() and __get() which directly asks for post meta keys (except for the following four keys: page_template, post_category, tags_input and ancestors). Here’s a quick example that shows the behavior: <?php $post_id = 42; $meta_key … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)