I
I
Ivan Medvedev2022-04-19 16:54:55
WordPress
Ivan Medvedev, 2022-04-19 16:54:55

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.
625ebe6cbb379915974243.png

<?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>


Do I understand correctly that I need to get the identifiers of the selected elements in the first and second selects and bind to them? How to implement all this? Where to dig?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question