1

I'm trying to create a sort of selection menu, in order to select custom posts by different custom taxonomies. I did the selection part, but I can't get to output my post and I can't understand where my mistakes are. What I have so fare: - Custom post type: "corsi"; - 5 different custom taxonomies: "prodotto", "livello", "ruolo", "stato"; - The selection form, made as a form, with radio button input and a bit of jQuery.

The form part

  <form id="regiration_form" novalidate action="<?php echo $current_url;?>" method="post">
    <fieldset>
        <div class="form-group">
            <h1>1</h1>
            <?php

    $prodotto_terms = get_terms( 'prodotto', array(
          'orderby'    => 'date',
          'order' => 'DESC',
          'hide_empty' => 0
        ) );
    ?>
                <?php foreach( $prodotto_terms as $term ) {
                          $term_link = get_term_link( $term );
                          echo '<input type="radio" name="prodotto" value="'.$term->slug.'">'.$term->name.'<br>';
                        }       

                    ?>

        </div>
        <input type="button" name="next" class="next btn btn-info" value="Next" />
    </fieldset>
    <fieldset>
        <div class="form-group">
            <h1>2</h1>
            <?php $livello_terms = get_terms( 'livello', array(
          'orderby'    => 'date',
          'order' => 'DESC',
          'hide_empty' => 0
        ) );
    ?>
                <?php foreach( $livello_terms as $term ) {
                          $term_link = get_term_link( $term );
                          echo '<input type="radio" name="livello" value="'.$term->slug.'">'.$term->name.'<br>';
                        }       

                    ?>


                    <?php $ruolo_terms = get_terms( 'ruolo', array(
          'orderby'    => 'date',
          'order' => 'DESC',
          'hide_empty' => 0
        ) );
             ?>
        </div>
        <input type="button" name="previous" class="previous btn btn-default" value="Previous" />
        <input type="button" name="next" class="next btn btn-info" value="Next" />
    </fieldset>
    <fieldset>
        <div class="form-group">
            <h1>3</h1>
            <?php foreach( $ruolo_terms as $term ) {
                          $term_link = get_term_link( $term );
                          echo '<input type="radio" name="ruolo" value="'.$term->slug.'">'.$term->name.'<br>';
                        }       

                    ?>

                            <?php $stato_terms = get_terms( 'stato', array(
          'orderby'    => 'date',
          'order' => 'DESC',
          'hide_empty' => 0
        ) );
            ?>
                    </div>
                    <input type="button" name="previous" class="previous btn btn-default" value="Previous" />
                    <input type="button" name="next" class="next btn btn-info" value="Next" />
                </fieldset>

                <fieldset>
                    <div class="form-group">
                        <h1>4</h1>
                        <?php foreach( $stato_terms as $term ) {
                          $term_link = get_term_link( $term );
                          echo '<input type="radio" name="stato" value="'.$term->slug.'">'.$term->name.'<br>';
                        }       

                    ?>

                    </div>
                    <input type="button" name="previous" class="previous btn btn-default" value="Previous" />
                    <input type="submit" name="submit" class="submit btn btn-success" value="Submit" />
                </fieldset>
            </form>

And this how I tried to output the posts, but nothing is showing up :/

 <?php    
                                if(isset($_REQUEST['submit']))
                                    { 

                                       $prodotto = $_POST["prodotto"];       
                                       $livello = $_POST["livello"];       
                                       $ruolo = $_POST["ruolo"];       
                                       $stato = $_POST["stato"]; 


                                    $soluzioni = new WP_Query( array( 
                                      'post_type' => 'corsi',
                                      'posts_per_page' => -1,
                                      'orderby' => 'title',
                                      'order'   => 'ASC',
                                      'paged' => $paged,
                                      'tax_query' => array(
                                        'relation' => 'AND',
                                         array(
                                           'taxonomy' => 'prodotto',
                                           'field' => 'slug',
                                           'terms' => $prodotto 
                                         ),
                                         array(
                                           'taxonomy' => 'livello',
                                           'field' => 'slug',
                                           'terms' => $livello
                                         )
                                      ) ) );


                                        if ( $soluzioni->have_posts() ) {
                                            echo '<ul>';
                                            while ( $soluzioni->have_posts() ) {
                                                $soluzioni->the_post();
                                                echo '<li>' . get_the_title() . '</li>';
                                            }
                                            echo '</ul>';

                                            wp_reset_postdata();
                                        } else {
                                            // no posts found
                                        }


                                                        } ?>

What am I doing wrong?

Thanks in advance, have a nice day :)

Lisa

3
  • 1
    Have you tried removing parameters from your query to see which ones are stopping it from working? And have you verified that all the data is indeed making it through correctly?
    – Tom J Nowell
    Commented Aug 1, 2018 at 16:49
  • Hi Tom, I just tried and apparently the problem was the "AND" relation. Seems to work now. Thanks for your help :)
    – Lisa
    Commented Aug 1, 2018 at 17:50
  • can you post that as an answer below?
    – Tom J Nowell
    Commented Aug 1, 2018 at 20:35

1 Answer 1

1

Apparently the problem was in the "AND" relation. Code now seems to work:

 <?php    
     if(isset($_REQUEST['submit']))
                { 
                    $prodotto = $_POST["prodotto"];       
                    $livello = $_POST["livello"];       
                    $ruolo = $_POST["ruolo"];       
                    $stato = $_POST["stato"]; 



                    $args = array( 
                            'post_type' => 'corsi',
                            'posts_per_page' => -1,
                            'orderby' => 'title',
                            'order'   => 'ASC',
                            'tax_query' => array(

                                array(
                                    'taxonomy' => 'prodotto',
                                    'field' => 'slug',
                                    'terms' => $prodotto,
                                         ),
                                array(
                                    'taxonomy' => 'livello',
                                    'field' => 'slug',
                                    'terms' => $livello,
                                         ),
                                 array(
                                    'taxonomy' => 'ruolo',
                                    'field' => 'slug',
                                    'terms' => $ruolo,
                                         ),
                              array(
                                    'taxonomy' => 'stato',
                                    'field' => 'slug',
                                    'terms' => $stato,
                                         ),


                                      ) 
                                    );
                    $soluzioni = new WP_Query($args);

                                if ( $soluzioni->have_posts() ) {
                                        echo '<ul>';
                                            while ( $soluzioni->have_posts() ) {
                                                $soluzioni->the_post();
                                                                    echo '<li>' . get_the_title() . '</li>';
                                                                            }
                                                        echo '</ul>';

                                                            wp_reset_postdata();
                                                                } else 
                                                                    {
                                                                           echo '<br/> OPPALLA no post';
                                                                    }




                             }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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