Make WordPress Core

Changeset 53733


Ignore:
Timestamp:
07/20/2022 03:39:18 PM (11 months ago)
Author:
SergeyBiryukov
Message:

Posts, Post Types: Correct the check for non-existing post in get_post_permalink().

The function was erroneously calling is_wp_error() on the result of a get_post() call, which returns null on failure, and never returns a WP_Error object.

Previously, passing a non-existing post ID to the function would result in a home URL being returned and a few Attempt to read property "post_type, post_name, hierarchical..." on null PHP warnings.

This commit ensures get_post_permalink() returns false on failure, which brings parity with get_permalink().

Includes a unit test to confirm the correct behavior.

Follow-up to [12923], [13023], [32606].

Props renegeuze, manzoorwani.jk.
Fixes #45329.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r53715 r53733  
    153153 * @param int|WP_Post $post      Optional. Post ID or post object. Default is the global `$post`.
    154154 * @param bool        $leavename Optional. Whether to keep post name or page name. Default false.
    155  * @return string|false The permalink URL or false if post does not exist.
     155 * @return string|false The permalink URL. False if the post does not exist.
    156156 */
    157157function get_the_permalink( $post = 0, $leavename = false ) {
     
    166166 * @param int|WP_Post $post      Optional. Post ID or post object. Default is the global `$post`.
    167167 * @param bool        $leavename Optional. Whether to keep post name or page name. Default false.
    168  * @return string|false The permalink URL or false if post does not exist.
     168 * @return string|false The permalink URL. False if the post does not exist.
    169169 */
    170170function get_permalink( $post = 0, $leavename = false ) {
     
    315315 * @param bool        $leavename Optional. Whether to keep post name. Default false.
    316316 * @param bool        $sample    Optional. Is it a sample permalink. Default false.
    317  * @return string|WP_Error The post permalink.
     317 * @return string|false The post permalink URL. False if the post does not exist.
    318318 */
    319319function get_post_permalink( $post = 0, $leavename = false, $sample = false ) {
     
    322322    $post = get_post( $post );
    323323
    324     if ( is_wp_error( $post ) ) {
    325         return $post;
     324    if ( ! $post ) {
     325        return false;
    326326    }
    327327
     
    39243924 *
    39253925 * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`.
    3926  * @return string|false The canonical URL, or false if the post does not exist or has not
    3927  *                      been published yet.
     3926 * @return string|false The canonical URL. False if the post does not exist
     3927 *                      or has not been published yet.
    39283928 */
    39293929function wp_get_canonical_url( $post = null ) {
Note: See TracChangeset for help on using the changeset viewer.