Exemplo n.º 1
0
/**
 * Assign a format to a post
 *
 * @since 0.0.1
 *
 * @param int|object $post   The post for which to assign a format.
 * @param string     $format A format to assign. Use an empty string or array to remove all formats from the post.
 * @return array|HQ_Error|false HQ_Error on error. Array of affected term IDs on success.
 */
function set_post_format($post, $format)
{
    $post = get_post($post);
    if (empty($post)) {
        return new HQ_Error('invalid_post', __('Invalid post.'));
    }
    if (!empty($format)) {
        $format = sanitize_key($format);
        if ('standard' === $format || !in_array($format, get_post_format_slugs())) {
            $format = '';
        } else {
            $format = 'post-format-' . $format;
        }
    }
    return hq_set_post_terms($post->ID, $format, 'post_format');
}
Exemplo n.º 2
0
/**
 * Set categories for a post.
 *
 * If the post categories parameter is not set, then the default category is
 * going used.
 *
 * @since 0.0.1
 *
 * @param int       $post_ID         Optional. The Post ID. Does not default to the ID
 *                                   of the global $post. Default 0.
 * @param array|int $post_categories Optional. List of categories or ID of category.
 *                                   Default empty array.
 * @param bool      $append         If true, don't delete existing categories, just add on.
 *                                  If false, replace the categories with the new categories.
 * @return array|bool|HQ_Error
 */
function hq_set_post_categories($post_ID = 0, $post_categories = array(), $append = false)
{
    $post_ID = (int) $post_ID;
    $post_type = get_post_type($post_ID);
    $post_status = get_post_status($post_ID);
    // If $post_categories isn't already an array, make it one:
    $post_categories = (array) $post_categories;
    if (empty($post_categories)) {
        if ('post' == $post_type && 'auto-draft' != $post_status) {
            $post_categories = array(get_option('default_category'));
            $append = false;
        } else {
            $post_categories = array();
        }
    } elseif (1 == count($post_categories) && '' == reset($post_categories)) {
        return true;
    }
    return hq_set_post_terms($post_ID, $post_categories, 'category', $append);
}