Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question
add comment

1 Answer

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.

share|improve this answer
    
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() –  DreamGrowers Nov 13 '12 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. –  toscho Nov 13 '12 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! –  DreamGrowers Nov 15 '12 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! –  DreamGrowers Nov 16 '12 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. –  toscho Nov 16 '12 at 5:15
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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