Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
Forked from anonymous/search.php
Last active November 28, 2016 14:22
Show Gist options
  • Save mgibbs189/d9e305e2557504852dd0381ce7b02b1a to your computer and use it in GitHub Desktop.
Save mgibbs189/d9e305e2557504852dd0381ce7b02b1a to your computer and use it in GitHub Desktop.
<?php
function touteslesbox_fwp_archive_query( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return $query;
remove_filter('pre_get_posts', 'touteslesbox_fwp_archive_query', 1);
if ( $query->is_post_type_archive( 'coupons' ) ) {
// get all posts declare in the ACF repeater field option
if( have_rows( 'option_coupons_box_top','option' ) ) :
$i = 0; $top_coupons_ID_array = array();
while( have_rows( 'option_coupons_box_top','option' ) ) : the_row();
$box = get_sub_field( 'box' ); $box_id = $box->ID;
// for each post, I get the last "coupon" associated to the post (key=related_box)
$the_query = new WP_Query ( array (
'posts_per_page' => 1,
'orderby' => 'date',
'post_type' => 'coupons',
'meta_query' => array( array( 'key' => 'related_box', 'value' => $box_id, 'compare' => '=' ) ),
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
));
if ( $the_query->have_posts() ) : while( $the_query->have_posts() && ++$i ) : $the_query->the_post();
$top_coupons_ID_array[] = get_the_ID() ;
switch ( $i ) { default: case 4: break; }
// if I get 4 "coupons", I stop the loop
endwhile; endif; wp_reset_postdata(); endwhile;
return $top_coupons_ID_array;
endif;
$query->set( 'posts_per_page', 14 );
$query->set( 'post_status', 'publish' );
$query->set( 'orderby', array ( 'date' => 'DESC' ) );
$query->set( 'post__in_not', $top_coupons_ID_array );
$query->set( 'facetwp', true );
}
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'page', 'test', 'box', 'actualites', 'coupons' ) );
}
add_filter( 'pre_get_posts', 'touteslesbox_fwp_archive_query', 1 );
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment