Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Last active December 27, 2015 16:19
Show Gist options
  • Save khoand0000/e13846753caaa07cc366 to your computer and use it in GitHub Desktop.
Save khoand0000/e13846753caaa07cc366 to your computer and use it in GitHub Desktop.
common wordpress functions

posts

get_posts([
            'meta_query' => [
            // 'relation' => 'AND', // Optional, defaults to "AND"

                [
                    'key' => '',
                    'value' => '', // array
                    //'type' => 'CHAR', // 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'
                    //'compare' => '=' // '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' and 'NOT EXISTS'. Default value is '='.
                ]
            ],
            'posts_per_page' => -1,
            'orderby' => 'date', //
            'order' => 'ASC', // 'DESC'
            'post_type' => 'post',
            // 'post_status' => 'publish', // default: 'publish', []
            // 'fields' => 'ids' // 'id=>parent'
        ]);

Post

  • get_post($post_id) return WP_Post
  • get_posts($arg) return array of WP_Post
  • WPFunctions::get_post($arg) return WP_Post, my function

Image

  • wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') return string HTML img element, include width and height attributes
  • wp_get_attachment_thumb_url($post_id) return Thumbnail URL
  • wp_attachment_is_image( $post ), $post is int|WP_Post

Attachment

  • wp_get_attachment_url($post_id)
  • get_attached_file($attachment_id, $unfiltered = false) return The file path
  • get_post_mime_type($post) $post is int|WP_Post return mime string (eg: image/png, text/csv, ...)

Sanitization

  • sanitize_file_name($file_name) remove special characters, still keep unicode characters (vietnamese character)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment