Skip to content

Instantly share code, notes, and snippets.

@gregoirenoyelle
Last active July 12, 2018 09:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregoirenoyelle/2469747 to your computer and use it in GitHub Desktop.
Save gregoirenoyelle/2469747 to your computer and use it in GitHub Desktop.
WordPress new WP Query
<?php
// article du codex: http://codex.wordpress.org/Class_Reference/WP_Query
// Query sur Catégories d'articles (ID) (inclure)
$ma_boucle = new WP_Query( 'cat=2,6,17,38' );
// Query sur Catégories d'articles (ID) (exclure)
$ma_boucle = new WP_Query( 'cat=-12,-34,-56' );
// Query sur Catégories d'articles (nom) et Author particulier (nom)
$ma_boucle = new WP_Query( 'category_name=staff,news&author_name=rami' );
// Query des articles qui ont les deux catégories
$ma_boucle = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );
// Query sur une page avec l'identifiant
$ma_boucle = new WP_Query( 'page_id=7' );
// Query sur une page avec le nom
$ma_boucle = new WP_Query( 'pagename=ma-page' );
// Query avec un array en notation simple
$ma_boucle = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );
?>
<?php
// article du codex: http://codex.wordpress.org/Class_Reference/WP_Query
// boucle pour appeler plusieurs catégorie avec un array
$ma_boucle_2 = new WP_Query (
array(
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( 4, 3 ) // category ID 4 et 3
)
),
'orderby' => 'title',
'posts_per_page' => -1
)
); // fin WP_Query
?>
<ul>
<?php
// début loop $ma_boucle
while ( $ma_boucle_2->have_posts() ) : $ma_boucle_2->the_post(); ?>
<!-- les éléments de la boucle se trouvent ici -->
<li><a href="<?php the_permalink();?>"><?php the_title(); ?></a> (article par <?php the_author(); ?>)</li>
<?php endwhile;
wp_reset_postdata();
// fin loop $ma_boucle
?>
</ul>
<?php
// article du codex: http://codex.wordpress.org/Class_Reference/WP_Query
$ma_boucle = new WP_Query (
array(
'cat' => 4,
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 3
)
); // fin WP_Query
?>
<ul>
<?php
// début loop $ma_boucle
while ( $ma_boucle->have_posts() ) : $ma_boucle->the_post(); ?>
<!-- les éléments de la boucle se trouvent ici -->
<li><a href="<?php the_permalink();?>"><?php the_title(); ?></a> (article par <?php the_author(); ?>)</li>
<?php endwhile;
wp_reset_postdata();
// fin loop $ma_boucle
?>
</ul>
<?php
// article du codex: http://codex.wordpress.org/Class_Reference/WP_Query
$ma_boucle = new WP_Query (
array(
'tax_query' => array( // bien mettre toutes les array
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob'
)
), // fin tax_query
'cat' => 4,
'cat__not_in' => array(2,3,7), // ID cat. toujours avec array
'post_parent' => '93', // ID Page pare
'posts_per_page' => 8,
'orderby' => 'title',
'order' => 'ASC',
'meta_key' => 'price',
'meta_value' => '22',
'meta_compare' => '<='
)
); // fin WP_Query
?>
<ul>
<?php
// début loop $ma_boucle
while ( $ma_boucle->have_posts() ) : $ma_boucle->the_post(); ?>
<!-- les éléments de la boucle se trouvent ici -->
<li><?php the_title(); ?></li>
<?php endwhile;
wp_reset_postdata();
// fin loop $ma_boucle
?>
</ul>
<?php
// Ici je sélectionne
// product où color=orange OU color=red et size=small cela donnera:
$args = array(
'post_type' => 'product',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'color',
'value' => 'orange',
'compare' => '=',
),
array(
'relation' => 'AND',
array(
'key' => 'color',
'value' => 'red',
'compare' => '=',
),
array(
'key' => 'size',
'value' => 'small',
'compare' => '=',
),
),
),
);
$ma_boucle = new WP_Query( $args );
?>
<ul>
<?php
// début loop $ma_boucle
while ( $ma_boucle->have_posts() ) : $ma_boucle->the_post(); ?>
<!-- les éléments de la boucle se trouvent ici -->
<li><?php the_title(); ?></li>
<?php endwhile;
wp_reset_postdata();
// fin loop $ma_boucle
?>
</ul>
<?php
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND', // ou 'OR'
array(
'taxonomy' => 'movie_genre',
'field' => 'slug',
'terms' => array( 'action', 'comedy' ),
),
array(
'taxonomy' => 'actor',
'field' => 'term_id',
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN', // 'IN' (Défaut), 'NOT IN', 'AND','EXISTS','NOT EXISTS'.
),
),
);
$ma_boucle = new WP_Query( $args );
?>
<ul>
<?php
// début loop $ma_boucle
while ( $ma_boucle->have_posts() ) : $ma_boucle->the_post(); ?>
<!-- les éléments de la boucle se trouvent ici -->
<li><?php the_title(); ?></li>
<?php endwhile;
wp_reset_postdata();
// fin loop $ma_boucle
?>
</ul>
<?php
// Template Name: Page avec boucle
//* Affichage nouvelle boucle après le contenu principal
add_action( 'genesis_entry_content', 'en_boucle_article', 15 );
function en_boucle_article() {
// Variables
$html = '';
// article du codex: http://codex.wordpress.org/Class_Reference/WP_Query
$ma_boucle = new WP_Query (
array(
'cat' => 4,
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 10
)
); // fin WP_Query
// début loop $ma_boucle
while ( $ma_boucle->have_posts() ) : $ma_boucle->the_post();
$html .= '<article class="contenu-boucle">';
$html .= sprintf( '<h3><a href="%s">%s</a></h3>', get_permalink(), get_the_title() );
$html .= sprintf( '<div class="visuel">%s</div>', get_the_post_thumbnail( $post->ID, 'medium') );
$html .= sprintf( '<div class="contenu-principal">%s</div>', get_the_excerpt() );
$html .= '</article>';
endwhile;
wp_reset_postdata();
// fin loop $ma_boucle
// Afficher le HTML
echo $html;
} // FIN function en_boucle_article()
genesis();
<?php
// Template Name: Page avec boucle
//* Affichage nouvelle boucle après le contenu principal
add_action( 'genesis_entry_content', 'en_boucle_article', 15 );
function en_boucle_article() {
// article du codex: http://codex.wordpress.org/Class_Reference/WP_Query
$ma_boucle = new WP_Query (
array(
'cat' => 4,
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 10
)
); // fin WP_Query
?>
<?php
// début loop $ma_boucle
while ( $ma_boucle->have_posts() ) : $ma_boucle->the_post(); ?>
<article class="contenu-boucle">
<h3><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h3>
<div class="visuel">
<?php the_post_thumbnail('medium' ); ?>
</div>
<div class="contenu-principal">
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile;
wp_reset_postdata();
// fin loop $ma_boucle
} // FIN function en_boucle_article()
genesis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment