0

I am using the Genesis Framework on my website, and have added the following code to my functions.php to customize the archive loop.

//* Archive Custom Loop
function archive_custom_loop() {

    echo '<div class="archive-pagination">';

next_posts_link('<div class="proigoumeno">Previous Posts &raquo;</div>', $loop ->max_num_pages);
previous_posts_link('<div class="epomeno">&laquo; Newer Posts</div>', $loop ->max_num_pages);

    echo '</div>';

    while (have_posts()) : the_post();
    ?>

<div class="lista"><a href="<?php the_permalink() ?>" target="_blank" style="font-weight: 700;"><?php the_title() ?></a><br><p class="entry-meta"><span class="entry-author"><?php the_author_posts_link(); ?></span><span class="provoles"><?php do_action( 'pageviews' ); ?> <img src="https://www.pcsteps.com/wp-content/uploads/2017/05/views.png" alt="Views"></span><span class="imerominia">Posted: <?php the_date() ?></span></p></div>

<div class="eikona"><a href="<?php the_permalink() ?>" target="_blank"><?php the_post_thumbnail( array(360, 200) ); ?></a></div>

<div class="perilipsi"><?php $content = get_the_excerpt(); echo mb_strimwidth($content, 0, 500, '...'); ?></div>

<div class="diaxwristiko"></div>

    <?php
    endwhile;

    echo '<div class="archive-pagination">';

next_posts_link('<div class="proigoumeno">Previous Posts &raquo;</div>', $loop ->max_num_pages);
previous_posts_link('<div class="epomeno">&laquo; Newer Posts</div>', $loop ->max_num_pages);

    echo '</div">';

}

What I would like is to select one author ID, and completely exclude it from the archives.

I have read the codex for the loop, but the syntax is a bit different, and I am not sure how to implement it.

1 Answer 1

1

You can do it with the pre_get_posts hook. In WP_Query there is an author parameter : https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters

pre_get_posts hook : https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

function wpse_288986_remove_post_from_author($query) {
    // We have to check if we are in front and if the query is the main query
    if(!is_admin() && $query->is_main_query() && is_archive())
    {
        $query->set('author', -2); // Where 2 is your user ID
    }
}
add_action('pre_get_posts', 'wpse_288986_remove_post_from_author');

EDIT : This code goes into your functions.php file

2
  • Thank you for your answer. Is there a problem if I don't add the "!is_admin() && $query->is_main_query()" part? the plain if(is_archive()) seems to work just fine. Commented Dec 19, 2017 at 21:41
  • is_admin() is for the posts list in your wp-admin, but it's not an archive page so yes, you can remove it. is_main_query is here to check if you're on the main query and not in another WP_Query. codex.wordpress.org/Function_Reference/is_main_query
    – Elex
    Commented Dec 20, 2017 at 6:27

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.