Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Forum: Hacks
    In reply to: WP_Query. Change posts order
    Thread Starter Motiejus Bagdonas

    (@motiejuss)

    Ok, I got it working, thank you.
    Here’s my working test example:

    <br />
    $all_posts = get_posts(array('numberposts' => -1, 'category' => $current_cat, 'orderby' => $orderby, 'order' => $order));<br />
    $all_post_ids = array();<br />
    foreach($all_posts as $post){<br />
      if(!in_array($post->ID, $sticky_post_ids)){<br />
        $all_post_ids[] = $post->ID;<br />
      }<br />
    }<br />
    $merged_all_post_ids = array_merge_recursive($sticky_post_ids,$all_post_ids);<br />

    Where $sticky_post_ids was a simple array with post ID’s like: array(12, 54, 32).
    And my WP_Query args: array( 'orderby' => 'post__in', 'post__in' => $merged_all_post_ids ).

    And I had to modify the plugin function a bit, which now looks like this:

    <br />
    function sort_query_by_post_in( $sortby, $thequery ) {<br />
            global $wpdb;<br />
            if ( ! empty( $thequery->query['post__in'] ) && isset( $thequery->query['orderby'] ) && $thequery->query['orderby'] == 'post__in' )<br />
                    $sortby = "find_in_set(" . $wpdb->prefix . "posts.ID, '" . implode( ',', $thequery->query['post__in'] ) . "')";<br />
            return $sortby;<br />
    }<br />
    add_filter( 'posts_orderby', 'sort_query_by_post_in', 10, 2 );<br />

    Forum: Hacks
    In reply to: WP_Query. Change posts order
    Thread Starter Motiejus Bagdonas

    (@motiejuss)

    Thank you Spencer, this seems promising.
    I guess now I will have to make another loop before WP_Query in order to construct an Array with post ID’s, right?

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