Home Plugin documentation Hooks and functions Functions pip_get_formatted_post_id()

 

 

Here we go!

 

pip_get_formatted_post_id()

Heads up!

This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don’t provide support for code customizations or 3rd party development.

Format ID depending on object type

 

pip/formatted_post_id

/**
 * Get formatted post ID
 *
 * @param bool $post_id
 *
 * @return bool|int|mixed|string|void
 * @example return term_6 for term ID
 */
function pip_get_formatted_post_id( $post_id = false ) {

    // If ID is specified, return
    if ( $post_id ) {
        return $post_id;
    }

    // Get current post ID
    $post_id = get_queried_object_id();

    if ( is_home() ) {

        // Blog
        $post_id = get_option( 'page_for_posts' );

    } elseif ( is_category() || is_tax() ) {

        // Custom taxonomies or category
        $post_id = 'term_' . $post_id;

    } elseif ( is_tag() ) {

        // Tags
        $post_id = 'post_tag_' . $post_id;

    } elseif ( is_post_type_archive() ) {

        $post_type = get_post_type() ? get_post_type() : get_queried_object()->name;

        // Custom post types
        if ( acf_get_options_page( $post_type . '-archive' ) ) {
            $post_id = $post_type . '_archive';
        }
    }

    $post_id = apply_filters( 'pip/formatted_post_id', $post_id );

    return $post_id;
}

You didn’t find the answer you were looking for?

or you think a specific topic deserve more informations?

 

Wave to bottom