Skip to content

Instantly share code, notes, and snippets.

@campusboy87
Created February 12, 2019 19:43
Show Gist options
  • Select an option

  • Save campusboy87/fee5994eebb75d6ef2436df842e34b3b to your computer and use it in GitHub Desktop.

Select an option

Save campusboy87/fee5994eebb75d6ef2436df842e34b3b to your computer and use it in GitHub Desktop.
<?php
add_filter( 'the_posts', 'prefix_the_posts', 10, 2 );
/**
* @param array $posts
* @param WP_Query $wp_query
*
* @return array
*/
function prefix_the_posts( $posts, $wp_query ) {
if ( $wp_query->is_main_query() && ! $wp_query->is_paged() && $wp_query->is_category( 'no-cat' ) ) {
$arr_posts = [];
$posts_ads = get_posts( [
'category_name' => 'ads',
'order' => 'ASC',
'orderby' => 'menu_order',
] );
foreach ( $posts_ads as $post_ads ) {
if ( $post_ads->menu_order > 0 ) {
$post_ads->menu_order = $post_ads->menu_order - 1;
$arr_posts[ $post_ads->menu_order ] = $post_ads;
}
}
foreach ( $posts as $post ) {
prefix_add_post( $post, $arr_posts );
}
return $arr_posts;
}
return $posts;
}
/**
* @param WP_Post $post
* @param array $arr_posts
*/
function prefix_add_post( $post, & $arr_posts ) {
static $offset = 0;
if ( isset( $arr_posts[ $offset ] ) ) {
++ $offset;
prefix_add_post( $post, $arr_posts );
} else {
$arr_posts[ $offset ] = $post;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment