All parameters of WP_Query and their explanations and practical cases

WP_Query is a powerful query tool provided by WordPress to get content related to the current page or article. Below are all the parameters of WP_Query and their explanations:

  1. 'author': query for articles by a specific author. Can be Author ID, Author Login, or Author Nickname.

Use case: Query all articles whose author is "John Smith".

$query = newWP_Query( array(
    'author_name' => 'john-smith'
) );
  1. 'cat': Search for articles in a specific category. Can be a category ID, category name or category slug.

Practical case: query all articles in the category "News".

$query = newWP_Query( array(
    'category_name' => 'news'
) );
  1. 'category__and': Query articles belonging to multiple categories at the same time. An array needs to be provided, containing multiple category IDs.

Practical case: Query articles belonging to category ID 2 and 3 at the same time.

$query = newWP_Query( array(
    'category__and' => array( 2, 3 )
) );
  1. 'category__in': Find articles belonging to any one of multiple categories. An array needs to be provided, containing multiple category IDs.

Use case: query articles belonging to category ID 2 or 3.

$query = newWP_Query( array(
    'category__in' => array( 2, 3 )
) );
  1. 'category__not_in': Find articles that do not belong to multiple categories. An array needs to be provided, containing multiple category IDs.

Use case: Search for articles that do not belong to category ID 2 or 3.

$query = newWP_Query( array(
    'category__not_in' => array( 2, 3 )
) );
  1. 'tag': Query articles with a specific tag. Can be a tag ID, tag name, or tag slug.

Use case: query all articles tagged with "WordPress".

$query = newWP_Query( array(
    'tag' => 'wordpress'
) );
  1. 'tag_id': Query articles with a specific tag. Must be a tag ID.

Practical case: query all articles with tag ID 5.

$query = newWP_Query( array(
    'tag_id' => 5
) );
  1. 'tag__and': Find articles with multiple tags at the same time. An array needs to be provided, containing multiple tag IDs.

Use case: Query all articles that contain both tag ID 5 and 6.

$query = newWP_Query( array(
    'tag__and' => array( 5, 6 )
) );
  1. 'tag__in': Find articles containing any of multiple tags. An array needs to be provided, containing multiple tag IDs.

Use case: Query all articles that contain tag ID 5 or 6.

$query = newWP_Query( array(
    'tag__in' => array( 5, 6 )
) );
  1. 'tag__not_in': Find articles that do not contain multiple tags. An array needs to be provided, containing multiple tag IDs.

Use case: Query articles that do not contain tag ID 5 or 6.

$query = newWP_Query( array(
    'tag__not_in' => array( 5, 6 )
) );
  1. 'post_type': query for a specific type of post. Can be any registered custom post type or the default post type.

Use case: Query all articles of type "portfolio".

$query = newWP_Query( array(
    'post_type' => 'portfolio'
) );
  1. 'post__in': query for a specific post. An array needs to be provided, containing multiple article IDs.

Use case: Query articles with ID 1, 3 and 5.

$query = newWP_Query( array(
    'post__in' => array( 1, 3, 5 )
) );
  1. 'post__not_in': query for posts other than a specific post. An array needs to be provided, containing multiple article IDs.

Use case: Query all articles except ID 1, 3 and 5.

$query = newWP_Query( array(
    'post__not_in' => array( 1, 3, 5 )
) );
  1. 's': Search for articles containing a specific keyword.

Use case: Query all articles containing the keyword "WordPress".

$query = newWP_Query( array(
    's' => 'wordpress'
) );
  1. 'date_query': query for articles published within a specific date range. An array needs to be provided, containing multiple date query parameters.

Use case: Query all articles published between January 1, 2022 and December 31, 2022.

$query = newWP_Query( array(
    'date_query' => array(
        'after'  => '2022-01-01',
        'before' => '2022-12-31',
    ),
) );
  1. 'meta_query': Query for articles with a specific custom field value. An array needs to be provided, containing multiple custom field query parameters.

Use case: query for all articles that have a custom field "featured" with a value of "yes".

$query = newWP_Query( array(
    'meta_query' => array(
        array(
            'key'   => 'featured',
            'value' => 'yes',
        ),
    ),
) );
  1. 'orderby': Specifies which parameter the result set is ordered by. Can be one of multiple sortable parameters.

Use case: Sort all articles by publishing date in descending order.

$query = newWP_Query( array(
    'orderby' => 'date',
    'order'   => 'DESC',
) );
  1. 'posts_per_page': Specifies the number of posts to display per page.

Practical case: display 10 articles per page.

$query = newWP_Query( array(
    'posts_per_page' => 10,
) );
  1. WP_Query loop query case

<?
    $args = array(
            'post_type' => 'post', 
            'orderby' => 'date', 
            'order' => 'DESC',   
            
    );
    $query = new WP_Query( $args );
?>

<?  if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); ?>

            <? the_title(); ?>
        
<?  }} else {echo '没有帖子';} wp_reset_postdata();  ?>

The above are some commonly used WP_Query parameters and their practical cases. WP_Query supports many other parameters, see the full list and usage in the WP_Query documentation.

Guess you like

Origin blog.csdn.net/qq_39339179/article/details/129443714