• Hey,

    on landingpage I let my users sort the posts the way the want it, to achieve this I created a dropdown looking like this:

    <div class="dropdown"> Sort by: &nbsp;
    <select id="myDropdown" name="sort-posts" onchange="document.location.href=location.href.split('?')[0]+this.options[this.selectedIndex].value;">
    <option value="?orderby=date&order=ASC">Date asc.</option>
    <option value="?orderby=date&order=DESC">Date desc.</option>
    ...

    This works fine for landing page, as there is no other ? in the URL that could be attacked by the javascript to split before.

    But I need a solution that works for every page no matter if there are other ? in the URL (like search page results or category/tag pages or any other dynamically created wp page…)

    So the javascript .split part is truly a makeshift.

    I need something like

    At first dropdown use, add the value, at second or continuing use replace the value instead of appending it to the end again.

    or with split function

    At first dropdown use, add the value, at second or continuing use split only the last ?

    If it were up to me, i wouldn’t use a dropdown, i would style it like a block nav, but thats further out of my imagination how to achieve this without the mentioned problems.

    Hope you understand.

    • This topic was modified 6 years, 1 month ago by bananenkopf.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    There’s no simple solution to this. String manipulation that must account for numerous possibilities is complicated. Look at all the source code for the PHP version of this, the function add_query_arg().

    I think what I would do is let the form submit to the same URL with option values being simple indicators of what was selected. 1, 2, 3 would work, or ‘date-asc’, ‘date-desc’, etc. Then sort out which query vars to set with PHP in the “pre_get_posts” action, based on the selected option.

    The action callback would do something like if query is the main query, switch on $_GET[‘sort-posts’].
    In the case of ‘date-asc’, set orderby to ‘date’ and set order as ‘ASC’
    In the case of ‘date-desc’, set….

    Avoiding the need for string manipulation is always a plus. String manipulation often results in inherently fragile code.

    Thread Starter bananenkopf

    (@bananenkopf)

    Hey bcworkz,

    thanks for your answer, I understood the way you are going to solve this problem and it looks like a solid solution to me!

    The problem is I am not that familiar with php much less the query functions.

    So I have to use add_query_arg() in combination with pre_get_posts() to correspond.

    Is there any way to get more detailed instructions on how to go on?

    Moderator bcworkz

    (@bcworkz)

    Uh, no, you misunderstood. Source code of add_query_arg() was just an example of how complicated accounting for all possibilities can get to add query args to an URL. It is unrelated to my proposed solution. “pre_get_posts” is not a function, it is a WordPress action. PHP hooks (callback functions) added to actions and related filters are the primary way we alter how WP works. If you are going to understand anything about PHP and WP, actions and filters are it. More on hooking actions and filters:
    https://developer.www.ads-software.com/plugins/hooks/

    Specifically for the pre_get_posts action:
    https://codex.www.ads-software.com/Plugin_API/Action_Reference/pre_get_posts

    The callback you add is passed a WP_Query object. The query vars you want to alter are the same as the arguments that are used when instantiating a WP_Query object. See
    https://codex.www.ads-software.com/Class_Reference/WP_Query

    Except you will change these using $query->set() method instead of passing to a new WP_Query like is shown on the class reference page. After you’ve digested all of that, if you still have questions, just ask!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘use ?orderby on dynamic pages iterative’ is closed to new replies.