Answer the question
In order to leave comments, you need to log in
How to get posts of the selected category?
Hello! I have a custom post type and their categories. There are two selects on the page, in the first we select a category, in the second - the post itself from this category.
I was able to display all categories and all posts. But, the task is to pass the posts of the selected category in the first select to the second select. You also need to get a link to the selected post.
<?php
$loc_cat_args = [
'taxonomy' => 'location-categories',
'type' => 'location',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'exclude' => '',
'include' => '',
'number' => 0,
'pad_counts' => false,
];
$loc_categories = get_categories( $loc_cat_args );
$locations = get_posts([
'post_type' => 'location',
'orderby' => 'name',
'order' => 'DESC',
//'suppress_filters' => true // 'posts_*' и 'comment_feed_*' фильтры игнорируются
])
?>
<select class="state">
<?php foreach( $loc_categories as $state ) : ?>
<option value="<?php echo $state -> name ?>"><?php echo $state -> name ?></option>
<?php endforeach ?>
</select>
<select>
<?php foreach( $locations as $loc_id => $loc ) : ?>
<option value="<?php echo $loc_id ?>"><?php echo $loc -> post_title ?></option>
<?php endforeach ?>
</select>
<a href="<?php ?>" class="book"><?php echo carbon_get_theme_option('btn')?></a>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question