WPLib_Posts::get_post_by PHP Method

get_post_by() static public method

static public get_post_by ( string $by, integer | string $value, array $args = [] ) : WP_Post
$by string
$value integer | string
$args array
return WP_Post
    static function get_post_by($by, $value, $args = array())
    {
        $_post = null;
        $criteria = array('post_status' => 'publish');
        switch ($by) {
            case 'slug':
            case 'post_name':
                $criteria['name'] = trim($value);
                break;
            case 'post_id':
            case 'post_ID':
            case 'id':
            case 'ID':
                $criteria['p'] = intval($value);
                break;
        }
        $query = new WP_Query(wp_parse_args($args, $criteria));
        if (count($query->posts)) {
            $_post = $query->post;
        }
        return $_post;
    }