I am using the function array_slice to insert two post within the loop in the index.

function insert_post_wpse_96347($posts) {
  global $wp_query;
  $args = array('meta_key' => 'Caja', 'meta_value' => array('UNO','DOS'), 'post__not_in' => get_option("sticky_posts"), 'posts_per_page' => '2','suppress_filters'=>true);

  if (is_main_query() && is_home() &&  0 == get_query_var('paged')) {
    $p2insert = new WP_Query($args);
    $insert_at = 0;
    if (!empty($p2insert->posts)) {
  return $posts;

And I have also created a plugin to show me the most viewed post. This plugin worked before the previous function added.

/* set view post */
function observePostViews($postID) {
  $count_key = 'post_views_count';
  $count = get_post_meta($postID, $count_key, true);
  if ($count=='') {
    $count = 0;
    delete_post_meta($postID, $count_key);
    add_post_meta($postID, $count_key, '0');
  } else {
    update_post_meta($postID, $count_key, $count);

My plugin is only one copy of the widget class-wp-widget-recent-posts.php and I've only changed the WP_Query using 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num' that I created as shown above.

In place of the widget, instead of displaying the post with more visits, it shows me the two post with the 'meta_key' => 'box', 'meta_value' => array('ONE','DOS')

How could resolve this conflict? Array_splice that only applied to the loop and not all query my home


Your code is quite messy which makes debugging hard, so I cleaned up your code a bit.

The posts_results filter accepts two parameters, one being the posts results array, and the second one is the current WP_Query instance. So you can pass the second argument and then test for your conditions.

I haven't tested your code, just cleaned it up and added the second parameter. The following should work: (REQUIRES PHP 5.4+)

add_filter( 'posts_results', 'insert_post_wpse_96347', 10, 2 );
function insert_post_wpse_96347( $posts, \WP_Query $q ) 
    remove_filter( current_filter(), __FUNCTION__ );

    if (    $q->is_main_query() 
         && $q->is_home() &&  
         0 == get_query_var( 'paged' ) 
     ) {

        $args = [
            'meta_key'         => 'Caja', 
            'meta_value'       => ['UNO','DOS'], 
            'post__not_in'     => get_option( "sticky_posts" ), 
            'posts_per_page'   => '2',
            'suppress_filters' =>true

        $p2insert = new WP_Query($args);
        $insert_at = 0;
        if ( !empty( $p2insert->posts ) ) {
            array_splice( $posts, $insert_at, 0, $p2insert->posts );
  return $posts;
  • I tried it and it worked!
    – nicogaldo
    Mar 3 '16 at 18:51
  • Glad it worked, enjoy ;-) Mar 3 '16 at 18:53
  • However these two post are repeated in the loop, how can I avoid that?
    – nicogaldo
    Mar 7 '16 at 18:13
  • Do you need to display two different posts on each page or just remove the custom posts as from page 2 onwards? Mar 7 '16 at 18:16
  • The idea is to highlight two news items that appear at the beginning of the loop, therefore should not appear below. I have asked before, but with the function described in this question, does not work.
    – nicogaldo
    Mar 7 '16 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.