- Итак, у вас есть настраиваемый тип сообщения под названием
your-post-type
- , и у вас есть настраиваемая таксономия под названием
your-custom-taxonomy
, и вы - хотите получить все сообщения с таксономией набор терминов
your-term
.
Вы делаете это правильно, задавая свои аргументы.
Примечание : если вы хотите получить все сообщения произвольного типа, вам не нужна вся 'tax_query'
часть кода.
Я добавил несколько комментариев, чтобы описать, что делает код:
$args = array( // define your arguments for query
'post_type' => 'your-post-type', // standard post type is 'post', you use a custom one
'tax_query' => array( // you check for taxonomy field values
array(
'taxonomy' => 'your-custom-taxonomy', // standard is 'category' you use a custom one
'field' => 'slug', // you want to get the terms by its slug (could also use id)
'terms' => 'your-term', // this is the taxonomy term slug the post has set
),
),
);
$loop = new WP_Query( $args ); // get post objects
// The Loop
if ( $loop ->have_posts() ) { // check if you received post objects
echo "<ul>"; // open unordered list
while ( $loop ->have_posts() ) { // loop through post objects
$loop ->the_post();
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; // list items
}
echo "</ul>"; // close unordered list
/* Restore original Post Data */
wp_reset_postdata(); // reset to avoid conflicts
} else {
// no posts found
}
Надеюсь, это поможет!
РЕДАКТИРОВАТЬ: Если вы не знаете, как использовать WP_Query
Этот код упорядочит ваши сообщения Wordpress по их заголовкам и вывод названия и содержания. Поместите это в файл шаблона вашей темы (узнайте что-нибудь о файлах шаблонов: https://developer.wordpress.org/themes/basics/template-hierarchy/).
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1, // limit the number of posts if you like to
'orderby' => 'title',
'order' => 'ASC'
);
$custom_query = new WP_Query($args);
if ($custom_query->have_posts()) : while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content();?>
<?php endwhile; else : ?>
<p>No posts</p>
<?php endif; wp_reset_postdata(); ?>
Вы сказали, что хотите использовать настраиваемый тип сообщения и выполнить таксономию запрос. Таким образом, вы можете настроить это, изменив аргументы в $args
.