Using Conditional Statement in functions.php

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;
}

Related posts

Leave a Reply

1 comment

  1. 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.