1

I've got this code below in my functions.php and I'd like to add two filters with conditional statement not to effect every query_posts. Something like this

if(!is_admin() && is_page('Home')){ //two filters }

However, I don't know PHP well enough to know where to put it or how to use the <?php> tags. Can anyone advise? It is from this thread that was closed in WP Forums.

add_filter('posts_orderby', 'edit_posts_orderby');
add_filter('posts_join_paged','edit_posts_join_paged');

function edit_posts_join_paged($join_paged_statement) {
    global $wpdb;
    $join_paged_statement = "LEFT JOIN ".$wpdb->prefix."wpv_voting ON ".$wpdb->prefix."wpv_voting.post_id = $wpdb->posts.ID";
    return $join_paged_statement;
}

function edit_posts_orderby($orderby_statement) {
    global $wpdb;
    $orderby_statement = "(".$wpdb->prefix."wpv_voting.vote_count) DESC";
    return $orderby_statement;
}

1 Answer 1

5

Basically, you put the second block into the first. And wait until the last moment:

add_action( 'pre_get_posts', 'wpse_71899_start_filter' );

function wpse_71899_start_filter()
{
    // wrong page
    if ( ! is_page( 'Home' ) )
        return; // stop here.

    add_filter('posts_orderby', 'edit_posts_orderby');
    add_filter('posts_join_paged','edit_posts_join_paged');
}

function edit_posts_join_paged($join_paged_statement) {
    global $wpdb;
    $join_paged_statement = "LEFT JOIN ".$wpdb->prefix."wpv_voting ON ".$wpdb->prefix."wpv_voting.post_id = $wpdb->posts.ID";
    return $join_paged_statement;
}

function edit_posts_orderby($orderby_statement) {
    global $wpdb;
    $orderby_statement = "(".$wpdb->prefix."wpv_voting.vote_count) DESC";
    return $orderby_statement;
}

In your theme’s functions.php should be only one <?php tag: at the first line.

You can place this code anywhere in that file, at the bottom for example.

11
  • Thanks for the support. I'm wondering how the final code should look. I'm not sure what the 71899 filter lines are? 'wpse_71899_start_filter' ); function wpse_71899_start_filter() Commented Nov 13, 2012 at 4:34
  • wpse_71899_start_filter() is a safe name for the function. We use this prefix here to prevent collisions with other peoples code.
    – fuxia
    Commented Nov 13, 2012 at 4:41
  • Couldn't figure out how to use these little comment boxes to display the full code so I answered below. Once you give it a glance, I'm going to post that exact code below. I just replaced that number with dg_filter - not sure if there's restrictions on what a function can be named. Thanks again! Commented Nov 15, 2012 at 5:25
  • IS there a way to past code into these comment boxes? If I paste the code I originally posted into functions.php, it works, but if I paste the suggested code with function wpse_71899_start_filter(), it breaks the category view. Thanks again! Commented Nov 16, 2012 at 4:47
  • Category pages are not affected due to the if ( ! is_page( 'Home' ) ). No idea what went wrong. Edit your question to document where you got stuck.
    – fuxia
    Commented Nov 16, 2012 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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