1

If I run this code I get the desired result

<?php
$terms = get_terms( 'call-type' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    foreach ( $terms as $term ) {
        echo '\'' . $term->name . '\', ';
    }
}
?>

result

'All', 'Completed', 'In progress', 'New',

But if I put the same code into an option all the select box it adds slashes to the results.

<select class="w-100" name="mf_term">
<option value="
<?php
$terms = get_terms( 'call-type' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    foreach ( $terms as $term ) {
        echo '\'' . $term->name . '\', ';
    }
}
?>
">All</option>

result

\'All\', \'Completed\', \'In progress\', \'New\',

How can I remove the slashes from the option in the select box

Update Code

<main>
        <section id="" class="py-3">
            <div id="" class="container-fuild">
                <!-- Start User Level Report -->
                <div id="" class="row mx-0 my-0">
                    <div class="col-lg-12">
                        <form action="" method="POST">
                            <div class="table-responsive ">
                                <table id="" class="table table-striped table-bordered text-center">
                                    <tbody>
                                        <tr>
                                            <td class="align-middle">
                                                Select Status<br /> 
                                                    <?php
                                                        $terms = get_terms( 'call-type' );
                                                        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
                                                          $term_option = '';
                                                          $sep = '';
                                                            foreach ( $terms as $term ) {
                                                               $term_option .=  $sep."'".$term->name."'";
                                                               $sep = ","; 
                                                            }
                                                            if(!empty($term_option))
                                                            {
                                                            ?>
                                                            <select class="w-100" name="mf_term">
                                                                <option value=""></option>      
                                                                <option value="<?php echo $term_option ;?>">All</option>
                                                                <?php
                                                                    $mf_term_args = array(
                                                                    'taxonomy'  =>  'call-type',
                                                                    ); 
                                                                    // Get Trems as array
                                                                    $term2s = get_categories( $mf_term_args );
                                                                    foreach ( $term2s as $term2 ) :
                                                                ?>
                                                                <option value="<?php echo $term2->name ?>"><?php echo $term2->name ?></option>
                                                                <?php endforeach; ?>
                                                            </select>
                                                            <?php
                                                            }
                                                          }
                                                    ?>
                                                    <?php
                                                        $mf_term = $_POST['mf_term'];
                                                        $mf_term = str_replace("\\\"", "\"", $mf_term);
                                                        $mf_term = str_replace("\\'", "'", $mf_term);
                                                        $mf_term = str_replace("\\\\", "\\", $mf_term);
                                                    ?>
                                            </td>

                                            <td class="align-middle">
                                                Select Department<br /> 
                                                <!-- Script Starts here -->
                                                <?php
                                                    $user_id = get_user_meta($user->ID);
                                                    $args = array(
                                                        'post_type' => 'department',
                                                        'posts_per_page' => -1,
                                                    );
                                                    $query = new WP_Query($args);
                                                ?>
                                                <select class="w-100" name="mf_department">Select Department</p>
                                                    <option value=""></option>
                                                    <option value="'Marketing', 'Maintenance'">
                                                            All
                                                        </option>
                                                    <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
                                                        <option value="<?php the_title(); ?>">
                                                            <?php the_title(); ?>
                                                        </option>
                                                    <?php endwhile; endif; ?>
                                                </select>
                                                <?php 
                                                    $mf_department = $_POST['mf_department'];
                                                    $mf_department = str_replace("\\\"", "\"", $mf_department);
                                                    $mf_department = str_replace("\\'", "'", $mf_department);
                                                    $mf_department = str_replace("\\\\", "\\", $mf_department);
                                                ?>
                                                <!-- Script Starts here -->
                                            </td>
                                            <td class="align-middle">
                                                Start Date<br />
                                                <input name="mf_start_date" type="date">
                                                <?php
                                                    $mf_start_date = $_POST['mf_start_date'];

                                                ?>
                                            </td>
                                            <td class="align-middle">
                                                End Date<br />
                                                <input name="mf_end_date" type="date">
                                                <?php
                                                    $mf_end_date = $_POST['mf_end_date'];
                                                ?>
                                            </td>
                                            <td class="align-middle">
                                                By Store 
                                            </td>
                                            <td class="align-middle">
                                                By Area
                                            </td>
                                            <td class="align-middle">
                                                By Region
                                            </td>
                                            <td class="align-middle">
                                                <button class="btn btn-lg btn-primary btn-block" type="submit">Run Query</button>
                                            </td>
                                        </tr>
                                        <tr class="">
                                            <td colspan="8">
                                                You have Selected Status: <strong class="text-danger"><?php echo $mf_term;?></strong> in the Department: <strong class="text-danger"><?php echo $mf_department;?></strong> from Starting Date: <strong class="text-danger"><?php echo $mf_start_date;?></strong> to Ending Date: <strong class="text-danger"><?php echo $mf_end_date;?></strong>.
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </form>
                    </div>
                    <div id="" class="col-lg-12">
                        <div class="table-responsive ">
                            <table id="data-table-1" class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th>Call Ref#</th>
                                        <th>Department</th>
                                        <th>Type of Call</th>
                                        <th></th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tfoot>
                                    <tr>
                                        <th>Call Ref#</th>
                                        <th>Department</th>
                                        <th>Col 3</th>
                                        <th>Col 4</th>
                                        <th>Col 5</th>
                                    </tr>
                                </tfoot>
                                <tbody class="text-center">
                                    <?php $query = new WP_Query(array(
                                        'posts_per_page'        =>  -1,
                                        'post-type'             =>  'call',

                                        'date_query' => array(
                                            array(
                                                'after' => $mf_start_date,
                                                'before' => $mf_end_date,
                                                'inclusive' => true,
                                            ),
                                        ),
                                        //'terms'               =>  'All, New',
                                        'tax_query' => array(
                                                    array(
                                                       'taxonomy' => 'call-type',
                                                        'field'    => 'name',
                                                        'terms'    => array( $mf_term ),
                                                        ),
                                                    ),
                                        'author'                =>  $current_user->ID,
                                        'meta_query'            =>  array(
                                                                array(
                                                                    'key'   =>  '_call_44',
                                                                    'value'   => [ $mf_department ],
                                                                ),
                                                                )
                                    ) ); ?>
                                    <?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
                                    <tr>
                                        <td class="align-middle">
                                            <?php echo $data = get_post_meta(get_the_ID(), '_call_22', true); ?>
                                        </td>
                                        <td class="align-middle">
                                            <?php echo $data = get_post_meta(get_the_ID(), '_call_44', true); ?>
                                        </td>
                                        <td class="align-middle">
                                            <?php echo $data = get_post_meta(get_the_ID(), '_call_45', true); ?>
                                        </td>
                                        <td class="align-middle">

                                        </td>
                                        <td class="align-middle">

                                        </td>
                                    </tr>
                                    <?php endwhile; endif; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <!-- End User Level Report -->
            </div>
        </section>
    </main>
  • 2
    did you want to add all terms in just single <option> tag ? – Chetan Vaghela Nov 13 '19 at 6:08
  • Where are you using this code? A function, shortcode, template, something else? I copied your exact code (just a different taxonomy) and it worked fine. Is this your exact code? – Jacob Peattie Nov 13 '19 at 6:21
  • Also, where are you checking the result? Are you actually looking at the value="" attribute in the HTML? Or are you looking at $_POST['mf_term'] or $_GET['mf_term']? – Jacob Peattie Nov 13 '19 at 6:22
  • Yes just looking for a single <option> tage with the resualt for 'All', 'Completed', 'In progress', 'New', the next part of the code take care of the single options – MFWebMaster Nov 13 '19 at 6:57
  • @MFWebMaster I have added an answer. please check answer and let me know if this works for you. it will add all terms separeated by comma and terms wrap with single quote – Chetan Vaghela Nov 13 '19 at 7:02
1

What you'd need to do is double-escape, so change echo '\'' . $term->name . '\', '; to echo '\\\'' . $term->name . '\\\', ';

...but, you'd be much better off just using different encapsulators:

<select class="w-100" name="mf_term">
<option value="
<?php
$terms = get_terms( 'call-type' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    foreach ( $terms as $term ) {
        echo "'{$term->name}', ";
    }
}
?>
">All</option>

As you're echo'ing a string that, not directly appending content to the HTML, you can use the double-quotes to encapsulate the output without having to worry about accidentlly exiting your select object early.

In addition, when using double-quotes with a standard PHP variable ($myVariable) or class / object property ($myClass->myProperty / $myObject->myProperty) you can just shove it in, and PHP will assess it correctly (I like to add curly braces for good measure, but it's not absolutely required); meaning you don't have to escape the string at all.

Finally; the code you've submitted will only create a single option in your dropdown, with the value of 'All', 'Completed', 'In progress', 'New',. If you're looking to have a list of options in your select box (so you can select All, Completed, In progress or New), the following may be more effective:

<select class="w-100" name="mf_term">

<?php
$terms = get_terms( 'call-type' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    foreach ( $terms as $term ) {
        echo "<option value='{$term->name}'>{$term->name}</option>";
    }
}
?>

</select>
  • Thank you but the double-escape, and different encapsulators option still add a '\' slash to the result. As for the last option it works but I need all of the Terms in one line with the quoute around then for the WP_Query meta_query – MFWebMaster Nov 13 '19 at 7:30
0

Here is a clean way to achieve your need. below code return option value like :

'All', 'Completed', 'In progress', 'New'

$terms = get_terms( 'call-type' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
  $term_option = '';
  $sep = '';
    foreach ( $terms as $term ) {
       $term_option .=  $sep."'".$term->name."'";
       $sep = ","; 
    }
    if(!empty($term_option))
    {
    ?>
    <select class="w-100" name="mf_term">
      <option value="<?php echo $term_option ;?>">All</option>
    </select>
    <?php
    }
  }
}
  • Hi Chetan thank you but it is not working getting the famous "The site is experiencing technical difficulties.: – MFWebMaster Nov 13 '19 at 7:19
  • I have tested this code and it is working fine for me. may be you are missing braces to close if statement. – Chetan Vaghela Nov 13 '19 at 7:33
  • Hi got it had to restructure the code a bit. But do you know why the $mf_term does not work for the 'tax_query' in the WP_Query. Also had to str_replace the slaches in the $mf_term – MFWebMaster Nov 13 '19 at 8:35
  • you are passing option value name of terms and in tax_query you are get field by slug. print a value of $mf_term. what data are you gettting ? – Chetan Vaghela Nov 13 '19 at 8:46
  • Not sure what you meen but here is the code'tax_query' => array( array( 'taxonomy' => 'call-type', 'field' => 'slug', 'terms' => array( $mf_term ), ), ), and it does not show any thing in the loop – MFWebMaster Nov 13 '19 at 9:08

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.