0

I have a taxonomy called projects and it has 4 types of projects. Assume new, current, past, future.

In taxonomy-projects.php page I need to show selected taxonomy term posts only. Assume I selected future category. Then I need to show all posts under future.

Here what I have tried so far. But it is returning all the created taxonomies under proejcts taxonomy and all posts assigned to those created taxonomies.

$projects = get_terms('projects');

foreach($projects as $project) {
    wp_reset_query();
    $args = array('post_type' => 'project',
        'tax_query' => array(
            array(
                'taxonomy' => 'projects',
                'field' => 'slug',
                'terms' => $project->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {

        echo '<h2>'.$project->name.'</h2>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
        endwhile;

     }
} 
  • 1
    You don't need to create your own query. The main query contains the current term's posts, you just need to run the default loop. – Milo Jun 27 '17 at 5:11
  • As @Milo said you don't need to create a new query. I think just checking the taxonomy term and putting each post under it's terms will solve the problem of yours. But one thing I am not sure that how you planned this "In taxonomy-projects.php page I need to show selected taxonomy term posts only. Assume I selected future category. Then I need to show all posts under future." ? – CodeMascot Jun 27 '17 at 6:18
  • @the_dramatist yes exactly. What ever term selected, posts only assigned to selected term should come. But with my code, all the terms and term related posts are coming. – user2584538 Jun 27 '17 at 8:25
  • Please share your taxonomy-projects.php source code, so we can modify the code get that functionality. @user2584538 – CodeMascot Jun 27 '17 at 8:30
  • @the_dramatist Your suggestion solved my problem. But how can I get taxonomy term in that way. ? I mean the title. In future term, Future as the title ? – user2584538 Jun 27 '17 at 8:36
0

Well, you can do it in many ways. You can introduce a form with a drop down input for the terms and pass it to a function through POST or GET method and the query the posts associated with the terms, then show them. Fro capturing the send data you can use admin_post_{$action}. Read thoroughly the documentation and do some search, you'll get necessary information to make this thing work.

You can use AJAX to make this thing more robust. For AJAXifing the system you can have a look at those below links-

  1. https://codex.wordpress.org/AJAX_in_Plugins
  2. https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)
  3. https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_nopriv_(action)

From your question this is the only thing right now I can suggest.

Hope those helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.