Last active
December 11, 2015 15:38
-
-
Save eduardozulian/4622108 to your computer and use it in GitHub Desktop.
Uses sticky posts as featured posts in the front page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Número de posts em destaque | |
$n = 5; | |
// Recebe os sticky posts e os ordena por ID de forma decrescente | |
$sticky_posts = get_option( 'sticky_posts' ); | |
rsort( $sticky_posts ); | |
// Verifica se há sticky posts | |
if ( $sticky_posts ) { | |
// Testa se já temos 5 posts fixos. Do contrário, preenchemos com posts cronológicos | |
if ( count( $sticky_posts ) < $n ) { | |
// Recebe os 5 últimos posts criados | |
$last_posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY ID DESC LIMIT $n" ) ); | |
// Combina os dois arrays e remove valores idênticos | |
$featured_posts = array_unique( array_merge( $sticky_posts, $last_posts ) ); | |
// Limita a no máximo 5 valores | |
$featured_posts = array_slice( $featured_posts, 0, $n ); | |
// A query | |
$my_query = new WP_Query( array( 'post__in' => $featured_posts ) ); | |
} | |
else { | |
$my_query = new WP_Query( array( 'post__in' => $sticky_posts, 'ignore_sticky_posts' => 1, 'posts_per_page' => $n ) ); | |
} | |
} | |
else { | |
$my_query = new WP_Query( array( 'posts_per_page' => $n ) ); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment