Description

Si le transient n'existe pas, n'a pas de valeur ou a expiré, la valeur retournée sera false.

Le paramètre $transient doit avoir au plus 172 caractères et WordPress lui rajoutera le préfixe '_transient_' ou '_transient_timeout_' dans la tables des options selon qu'il a expiré ou non.

Paramètre

$transient

(string) (Requis) Nom du transient.

Retourne

(mixed) Valeur du transient.

Structure de la fonction get_transient()

Définie dans le fichier wp-includes/option.php à la ligne 766 :

function get_transient( $transient ) {

    /**
     * Filters the value of an existing transient before it is retrieved.
     *
     * The dynamic portion of the hook name, `$transient`, refers to the transient name.
     *
     * Returning a truthy value from the filter will effectively short-circuit retrieval
     * and return the passed value instead.
     *
     * @since 2.8.0
     * @since 4.4.0 The `$transient` parameter was added
     *
     * @param mixed  $pre_transient The default value to return if the transient does not exist.
     *                              Any value other than false will short-circuit the retrieval
     *                              of the transient, and return that value.
     * @param string $transient     Transient name.
     */
    $pre = apply_filters( "pre_transient_{$transient}", false, $transient );

    if ( false !== $pre ) {
        return $pre;
    }

    if ( wp_using_ext_object_cache() ) {
        $value = wp_cache_get( $transient, 'transient' );
    } else {
        $transient_option = '_transient_' . $transient;
        if ( ! wp_installing() ) {
            // If option is not in alloptions, it is not autoloaded and thus has a timeout.
            $alloptions = wp_load_alloptions();
            if ( ! isset( $alloptions[ $transient_option ] ) ) {
                $transient_timeout = '_transient_timeout_' . $transient;
                $timeout           = get_option( $transient_timeout );
                if ( false !== $timeout && $timeout < time() ) {
                    delete_option( $transient_option );
                    delete_option( $transient_timeout );
                    $value = false;
                }
            }
        }

        if ( ! isset( $value ) ) {
            $value = get_option( $transient_option );
        }
    }

    /**
     * Filters an existing transient's value.
     *
     * The dynamic portion of the hook name, `$transient`, refers to the transient name.
     *
     * @since 2.8.0
     * @since 4.4.0 The `$transient` parameter was added
     *
     * @param mixed  $value     Value of transient.
     * @param string $transient Transient name.
     */
    return apply_filters( "transient_{$transient}", $value, $transient );
}

Fonctions et Hooks utilisés par get_transient()

wp_installing()

Vérifie ou met Wordpress en mode installation.

wp_cache_get()

Retourne les contenus du cache en donnant la clé et le groupe.

wp_using_ext_object_cache()

Alterne $_wp_using_ext_object_cache sur on ou off sans directement toucher la global.

apply_filters()

Appel les fonctions qui ont été attaché à un filtre (hook).

pre_transient_{$transient}

Filtre la valeur d'un transient existant avant qu'il ne soit retourné.

transient_{$transient}

Filtre la valeur d'un transient existant.

wp_load_alloptions()

Charge et met en cache toutes les options auto-chargées si disponible ou toutes les options.

delete_option()

Supprime une option par son nom. Empêche de supprimer les options protégées par WordPress.

get_option()

Retourne une valeur d'option en fonction de son nom.

Où trouver la fonction get_transient() dans le CMS Wordpress

Exemple

Utilisation de get_transient, set_transient et WP_Query :
if( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) {
    // S'il n'existe pas, régénère les données et enregistre le transient
    $special_query_results = new WP_Query( 'cat=5&order=random&tag=tech&post_meta_key=thumbnail' );
    set_transient( 'special_query_results', $special_query_results );
}

Sources

Codex Wordpress : get_transient()

Autres fonctions dans le même fichier : wp-includes/option.php

Retour