WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to write a pre_get_post() (3 posts)

  1. nobita
    Member
    Posted 1 year ago #

    How to write which is recommended. Please let me know.

    example 1

    use WordPress functions.

    add_action( 'pre_get_posts', 'category_display_per_3' );
    
    function category_display_per_3( ) {
     if ( is_main_query() and is_category() ) {
         set_query_var( 'posts_per_page', 3 );
     }
    }

    example 2

    object access.

    add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
    
    function foo_modify_query_exclude_category( $query ) {
     if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )
     $query->set( 'cat', '-5' );
    }

    codex@source

    I am sorry to be poor at English.

    Thank you.

  2. Option 2 would be the way to go.

  3. nobita
    Member
    Posted 1 year ago #

    Thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.