web-dev-qa-db-fra.com

Stocker les identifiants de post-it dans un transitoire

Je développe un thème qui comporte une section de publications en vedette avant la boucle principale des publications. Cool et frais, non? :) Tous les posts collants avec une image sélectionnée doivent être affichés dans cette section et exclus de la boucle principale. Peasy facile. En faisant mes recherches, je suis tombé sur un article assez ancien sur le sujet que j’ai trouvé très utile. Il recommande de ne pas utiliser query_posts, comme je l'ai déjà fait, et propose une approche plus élégante:

    /**
    * Filter the home page posts, and remove any featured post ID's from it. Hooked
    * onto the 'pre_get_posts' action, this changes the parameters of the query
    * before it gets any posts.
    *
    * @global array $featured_post_id
    * @param WP_Query $query
    * @return WP_Query Possibly modified WP_query
    */
    function itheme2_home_posts( $query = false ) {

      // Bail if not home, not a query, not main query, or no featured posts
      if ( ! is_home() || ! is_a( $query, 'WP_Query' ) || ! $query->is_main_query() || ! itheme2_featuring_posts() )
      return;

      // Exclude featured posts from the main query
      $query->set( 'post__not_in', itheme2_featuring_posts() );

      // Note the we aren't returning anything.
      // 'pre_get_posts' is a byref action; we're modifying the query directly.
    }
    add_action( 'pre_get_posts', 'itheme2_home_posts' );

    /**
    * Test to see if any posts meet our conditions for featuring posts.
    * Current conditions are:
    *
    * - sticky posts
    * - with featured thumbnails
    *
    * We store the results of the loop in a transient, to prevent running this
    * extra query on every page load. The results are an array of post ID's that
    * match the result above. This gives us a quick way to loop through featured
    * posts again later without needing to query additional times later.
    */
    function itheme2_featuring_posts() {
      if ( false === ( $featured_post_ids = get_transient( 'featured_post_ids' ) ) ) {

        // Proceed only if sticky posts exist.
        if ( get_option( 'sticky_posts' ) ) {

          $featured_args = array(
            'post__in'      => get_option( 'sticky_posts' ),
            'post_status'   => 'publish',
            'no_found_rows' => true
          );

          // The Featured Posts query.
          $featured = new WP_Query( $featured_args );

          // Proceed only if published posts with thumbnails exist
          if ( $featured->have_posts() ) {
            while ( $featured->have_posts() ) {
              $featured->the_post();
              if ( has_post_thumbnail( $featured->post->ID ) ) {
                $featured_post_ids[] = $featured->post->ID;
              }
            }

            set_transient( 'featured_post_ids', $featured_post_ids );
          }
        }
      }

      // Return the post ID's, either from the cache, or from the loop
      return $featured_post_ids;
    }

Tout fonctionne très bien sauf les passagers. Je n'arrive pas à comprendre comment réinitialiser le transitoire si sticky_posts a été mis à jour. Probablement utiliser le hook updated_option? J'apprécierais toute aide.

1
Nikita

Tu peux essayer:

add_action('update_option_sticky_posts', function( $old_value, $value ) {
         $featured_args = array(
            'post__in'      => $value,
            'post_status'   => 'publish',
            'no_found_rows' => true
          );

          // The Featured Posts query.
          $featured = new WP_Query( $featured_args );

          // Proceed only if published posts with thumbnails exist
          if ( $featured->have_posts() ) {
            while ( $featured->have_posts() ) {
              $featured->the_post();
              if ( has_post_thumbnail( $featured->post->ID ) ) {
                $featured_post_ids[] = $featured->post->ID;
              }
            }

            set_transient( 'featured_post_ids', $featured_post_ids );
          }
}, 10, 2);

Je suppose que vous avez les ID de poste sur l'option sticky_posts c'est pourquoi vous interrogez avec cela dans le WP_Query

1
Drupalizeme

La classe WP_Query offre un paramètre permettant d'inclure/exclure les posts collants. Il n'est pas nécessaire de stocker leurs identifiants où que ce soit. Il suffit de créer une nouvelle requête et de générer les posts collants contenant une image sélectionnée:

$sticky = get_option( 'sticky_posts' );
$query = new WP_Query( array( 'p' => $sticky ) );

Maintenant, vous pouvez créer une autre boucle et exclure les posts collants:

$query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );

Vous pouvez également définir le 'ignore_sticky_posts' => 1 pour ignorer les posts collants de votre boucle.

C'est tout ce dont vous avez besoin.

0
Jack Johansson