0

I'm trying to add a meta to wp_get_archives

add_filter('getarchives_where','my_archives_filter');

function my_archives_filter($where_clause) {

    if(!is_user_logged_in()){
        return "WHERE post_type = 'post' AND post_status = 'publish'";
    }else{
        return $where_clause;
    }

}

I want to add the distinction of getting posts that have the meta private_page='0' when The user is not logged in, but I can't find how to put the meta distinction in the return "WHERE post_type = 'post' AND post_status = 'publish'";. The line before is what I tried, and it returns nothing.

0

1) You need to include postmeta into query that you're using for archive pages in a kind of this way:

add_filter('getarchives_join', 'my_archives_join_filter');

function my_archives_join_filter($join) {
    if (!is_user_logged_in()) {
        global $wpdb;
        return $join . "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)";
    }
    return $join;
}

2) You need to include condition to select a post with specific metafield in this way:

add_filter('getarchives_where', 'my_archives_where_filter');

function my_archives_where_filter($where_clause) {
    if (!is_user_logged_in()) {
        global $wpdb;
        $where_clause = "WHERE $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish'";
        return $where_clause . "AND $wpdb->posts.meta_key = 'private_page' AND $wpdb->posts.meta_value = '0'";
    } 
     return $where_clause;
}

Code where not tested for your specific case. But the approach in general should be clear.

UPDATE: where curious enough to test an approach. All works ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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