WordPress Custom Query - Part 06 - tax_query.mp4 Lấy bài viết thuộc taxonomy custom (ok)

Kết quả đều giống nhau

Ví dụ 1:

C:\xampp\htdocs\wordpress\wp-content\themes\twentytwentyone\index.php

<?php
	get_header();
?>
<?php 
	$args = array(
		'post_type' => 'post',
		'posts_per_page' => -1,
		'cat' => 9
	);
	$query = new WP_Query($args);
	$i = 1;
	while ($query->have_posts()): $query->the_post();
	echo $i;
	?>
		<h5><?php the_title(); ?></h5>
		<p><?php the_tags(); ?></p>
	<?php
	$i++;
	endwhile;
	wp_reset_query();
?>
<?php
	get_footer();
?>

Ví dụ 2:

C:\xampp\htdocs\wordpress\wp-content\themes\twentytwentyone\index.php

<?php
	get_header();
?>
<?php 
	$args = array(
		'post_type' => 'post',
		'posts_per_page' => -1,
		'tax_query' => array(
			array(
        'taxonomy' => 'category',                //(string) - Tên của taxonomy
        'field' => 'slug',                    //(string) - Loại field cần xác định term của taxonomy, sử dụng 'id' hoặc 'slug'
        'terms' => array( 'nostrum-et-maiores-perspiciatis-est-qui'),    //(int/string/array) - Slug của các terms bên trong taxonomy cần lấy bài
        'include_children' => true,           //(bool) - Lấy category con, true hoặc false
        'operator' => 'IN'                    //(string) - Toán tử áp dụng cho mảng tham số này. Sử dụng 'IN' hoặc 'NOT IN'
      ),
		)
	);
	$query = new WP_Query($args);
	$i = 1;
	while ($query->have_posts()): $query->the_post();
	echo $i;
	?>
		<h5><?php the_title(); ?></h5>
		<p><?php the_tags(); ?></p>
	<?php
	$i++;
	endwhile;
	wp_reset_query();
?>
<?php
	get_footer();
?>

Ví dụ 3: Lấy bài viết thuộc taxonomy custom

C:\xampp\htdocs\wordpress\wp-content\themes\twentytwentyone\index.php

<?php
	get_header();
?>
<?php 
	$args = array(
		'post_type' => 'post',
		'posts_per_page' => -1,
		'tax_query' => array(
			array(
        'taxonomy' => 'lionel',                //(string) - Tên của taxonomy
        'field' => 'term_id',                    //(string) - Loại field cần xác định term của taxonomy, sử dụng 'id' hoặc 'slug'
        'terms' => array( 31),    //(int/string/array) - Slug của các terms bên trong taxonomy cần lấy bài
        'include_children' => true,           //(bool) - Lấy category con, true hoặc false
        'operator' => 'IN'                    //(string) - Toán tử áp dụng cho mảng tham số này. Sử dụng 'IN' hoặc 'NOT IN'
      ),
		)
	);
	$query = new WP_Query($args);
	$i = 1;
	while ($query->have_posts()): $query->the_post();
	echo $i;
	?>
		<h5><?php the_title(); ?></h5>
		<p><?php the_tags(); ?></p>
	<?php
	$i++;
	endwhile;
	wp_reset_query();
?>
<?php
	get_footer();
?>

Ví dụ 4: Mở rộng của ví dụ 3

C:\xampp\htdocs\wordpress\wp-content\themes\twentytwentyone\index.php

<?php
	get_header();
?>
<?php 
	$args = array(
		'post_type' => 'post',
		'posts_per_page' => -1,
		'tax_query' => array(
			array(
        'taxonomy' => 'lionel',                //(string) - Tên của taxonomy
        'field' => 'name',                    //(string) - Loại field cần xác định term của taxonomy, sử dụng 'id' hoặc 'slug'
        'terms' => array( 'Lionel1'),    //(int/string/array) - Slug của các terms bên trong taxonomy cần lấy bài
        'include_children' => true,           //(bool) - Lấy category con, true hoặc false
        'operator' => 'IN'                    //(string) - Toán tử áp dụng cho mảng tham số này. Sử dụng 'IN' hoặc 'NOT IN'
      ),
		)
	);
	$query = new WP_Query($args);
	$i = 1;
	while ($query->have_posts()): $query->the_post();
	echo $i;
	?>
		<h5><?php the_title(); ?></h5>
		<p><?php the_tags(); ?></p>
	<?php
	$i++;
	endwhile;
	wp_reset_query();
?>
<?php
	get_footer();
?>

Last updated