Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Here is an alternative method that I used on another site. The gist is to always force the sticky on top, which is kind of what it’s supposed to do, but the sticky post was no longer a “recent” post, and it was not getting picked up by the query. This may not be the best solution, but it worked for me (I ONLY have access to this template file, and not the WP core files). $categories was set previously in the program for a specific one….

    $recent_posts = new WP_Query(array(
    				'showposts' => $posts,
    				'post_type' => 'post',
    				'ignore_sticky_posts' => 'false',
    				'post__not_in' => get_option('sticky_posts'),
    				'cat' => $categories,
    			));
    
    			$recent_sticky_posts = new WP_Query(array(
    				'showposts' => $posts,
    				'post_type' => 'post',
    				'ignore_sticky_posts' => 'false',
    				'post__in' => get_option('sticky_posts'),
    				'cat' => $categories,
    			));
    
    			$my_regposts = array();
    			$my_stickyposts = array();
    
    			foreach($recent_sticky_posts->posts as $my_post) {
    				array_push($my_stickyposts,$my_post);
    			}
    
    			foreach($recent_posts->posts as $my_post) {
    				array_push($my_regposts,$my_post);
    			}
    
    			// clear out the original query and push on the regular and sticky posts
    			$recent_posts->posts = array();
    
    			// sticky posts first
    			foreach($my_stickyposts as $my_stickypost) {
    				array_push($recent_posts->posts,$my_stickypost);
    			}
    			// regular posts next
    			foreach($my_regposts as $my_regpost) {
    				array_push($recent_posts->posts,$my_regpost);
    			}

    What this bit of code replaced was just a normal get of recent posts. The regular version was not getting the sticky ones (because they were no longer recent)

    Greencode, your second query should be:

    $wp_query = new WP_Query( array( 'posts_per_page' => '10', 'ignore_sticky_posts' => 1, 'post__not_in' => get_option('sticky_posts'), 'paged' => $paged, 'cat' => 275));

    not

    $wp_query = new WP_Query( array( 'posts_per_page' => '10', 'ignore_sticky_posts' => 1, 'post__not_in' => $sticky, 'paged' => $paged, 'cat' => 275));

    Nowhere in your example, do you set the $sticky variable.

Viewing 2 replies - 1 through 2 (of 2 total)