2

I'm having a hard time determining the first direction to take with this. I'm trying to write a plugin that allows users to pick a location for a post to be inserted into the homepage. For example a user can 'sticky' an old post to the 2nd location on their front page. My problem is I don't know how to insert a post into the loop at a specific point. I'm thinking it should be something like the following (but clearly I'm missing the insert text):

$current_position = $_POST('current_postition');
add_action ('pre_get_posts', 'add_post_to_frontpage');
function add_post_to_frontpage ($query) {
  if ( $query->is_home() && $query->is_main_query() && $query->current_position = $positionlocation ) { 
        //insert a given post into this current_position
     $query->set( 'p=$desired_post');
  }
}

Thanks

EDIT: I added in what I think is a logical way to set the post, but think that this will cause trouble with the posts that follow the post I'm inserting

  • I haven't tested it but I pretty sure that what you did-- $query->set( 'p=$desired_post');-- won't work. It should be $query->set('p',$desired_post); (also untested). I am pretty sure that that will cause the whole loop to be composed of that single post, which isn't what you want. What you are asking is pretty easy if you edit the Loop, but doing this with a plugin could be tricky. I've been thinking about it though. – s_ha_dum Apr 18 '13 at 20:10
  • s_ha_dum ya it pretty much does exactly what you said. I thought it would set the post at current_location to the $desired_post but it makes the loop a single post... Any other thoughts? and Thank you! – bob_hardy Apr 19 '13 at 14:07
2

This took a bit of trial and error but I think I got it. I was getting an infinite loop until I added suppress_filters. After that it was short work.

function insert_post_wpse_96347($posts) {
  global $wp_query;
  $desired_post = 151;
  if (is_main_query() && is_home() &&  0 == get_query_var('paged')) {
    $p2insert = new WP_Query(array('p'=>$desired_post,'suppress_filters'=>true));
    $insert_at = 3;
    if (!empty($p2insert->posts)) {
      array_splice($posts,$insert_at,0,$p2insert->posts);
    }
  }
  return $posts;
}
add_filter('posts_results','insert_post_wpse_96347');

This will force a post, which I have hard-coded, into position 3, again hard-coded. I don't know how you are saving, or plan to save, your configuration values but that is is the basic "insert" function. This will increase the post count for the first page above the configured value but otherwise preserves normal pagination as far as I can tell. It is tested but barely.

  • Hey s_ha_dum, thanks! I'm taking some of the concepts that you shared here and I'm putting my own slight tweak to it. Once it's done I'll share it. Couldn't have done it without your help though, thanks! – bob_hardy Apr 22 '13 at 15:11
  • Idea: get_post would be a great candidate here as you are inseting one post only :-). – Pieter Goosen Jan 10 '15 at 4:59

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.