0

I want to add some custom query string (when click on a column of a table) in the current url which has multiple query string in it. i have tried a lot to restructure the url but failed to do so. here some code which i have tried

global $wp;
    
    // get current url with query string.
    $current_url =  home_url( $wp->request ); 

my html code

<tr>
                            <th class="table-heading directory-first-column table-sorting" scope="col">
                                <?php
                                if (isset($_GET) && $_GET['order'] == 'desc') {
                                    $order = 'asc';
                                } else {
                                    $order = 'desc';
                                }
                                ?>
                                <a href="<?php echo $current_url.(!empty($_GET) && isset($_GET)) ? '&' : '?'; ?>sortby=last_name&order=<?php echo $order; ?>" class="sort-link">Profile Information</a>
                            </th>
                            <th class="table-heading table-sorting" scope="col">
                                <?php
                                if (isset($_GET) && $_GET['order'] == 'desc') {
                                    $order = 'asc';
                                } else {
                                    $order = 'desc';
                                }
                                ?>
                                <a href="<?php echo $current_url.(!empty($_GET) && isset($_GET)) ? '&' : '?'; ?>sortby=host_university&order=<?php echo $order; ?>" class="sort-link">Host University</a>
                            </th>
                            <th class="table-heading table-sorting" scope="col">
                                <?php
                                if (isset($_GET) && $_GET['order'] == 'desc') {
                                    $order = 'asc';
                                } else {
                                    $order = 'desc';
                                }
                                ?>
                                <a href="<?php echo $current_url.(!empty($_GET) && isset($_GET)) ? '&' : '?'; ?>sortby=field_of_study&order=<?php echo $order; ?>" class="sort-link">Field of Study</a>
                            </th>
                            <th class="table-heading directory-last-column table-sorting" scope="col">
                                <?php
                                if (isset($_GET) && $_GET['order'] == 'desc') {
                                    $order = 'asc';
                                } else {
                                    $order = 'desc';
                                }
                                ?>
                                <a href="<?php echo $current_url.(!empty($_GET) && isset($_GET))  ? '&' : '?'; ?>sortby=program_year&order=<?php echo $order; ?>" class="sort-link">Program Year</a>
                            </th>
                        </tr>

how could i add & or ? in the url, if there is not query string then add ? if url has some query string the add & into it. any suggestion on it.

Manish Agarwal
  • 166
  • 2
  • 12

0 Answers0