Support » Developing with WordPress » Query string URL parameter in wp-admin custom page ?

  • Resolved profnox

    (@profnox)


    Hi,

    I’m developping a specific plugin to display a custom page in a custom post-type (vehicle) which should show a table of sold vehicles and their details, by years.

    In order to display the table corresponding to a desired year, I wanted to use a query string parameter (GET) in the page URL, so I could create a list of links with this “&sellyear=value” parameter for each existing years.

    Ex : http:// localhost/lywebsite/wp-admin/edit.php?post_type=vehicle&page=eai_admin_vehicles_sales_submenu&sell_year=2018

    What I tried is to declare a new query var in my plugin file :

    add_filter( 'init', 'add_sellyear_query_var' );
    function add_sellyear_query_var()
    {
        global $wp;
        $wp->add_query_var( 'sellyear' );
    }
    $sellyear = get_query_var( 'sellyear' );

    But when I refresh the page, I got an error :

    Fatal error: Uncaught Error: Call to a member function get() on null in C:\wamp\www\mywebsite\wp-includes\query.php

    I’ve searched a lot on the web, but what I got is that, it seems not possible to use custom query string parameter in wp-admin… However the threads were pretty old (2012/2013) so I wonder if it is still impossible today ?
    If it’s the case, what alternative can I try ?
    If not, what is the correct way to use custom $_GET parameter in wp-admin page ?

    Thank you in advance,

    N.

    • This topic was modified 1 year, 3 months ago by profnox.
    • This topic was modified 1 year, 3 months ago by profnox.
    • This topic was modified 1 year, 3 months ago by profnox.
Viewing 5 replies - 1 through 5 (of 5 total)
  • You can, but you have to use them correctly. And you can’t use variables before they are ready, like $wp. I think at init, it isn’t what you want.
    See this list.

    Hi Joy,

    Thank you for your help.

    Yes, I tried with different filter tags and priorities, but the result was always the same.
    Even if I don’t try to get the query var (just set it), I got some errors pointing the wp-includes folder. I don’t even know how to “wait” for $wp variable to be ready.
    I notice that the filter works on front pages, but not on admin pages.

    I’m originaly not a backend developer, I apologize for my misunderstanding and my poor practice… 🙁

    I’ve been stucked all morning, so I finally resolved to use the native PHP $_GET[”] variable.

    $sellyear = $_GET['sellyear'] ? $sellyear = $_GET['sellyear'] : 2018;

    When I call it in my the function which display the admin page (which is call by add_submenu_page method), it seems to work.
    However, the way I tried the filter above (the one in my first post) it returns an empty string (”).

    I know it should not be the most correct way, but I didn’t find another way.
    I stay open if you have something to propose me.

    Thank you for your help,

    N.

    Moderator bcworkz

    (@bcworkz)

    It’s OK to pull from the $_GET array, as long as you properly validate and sanitize the passed value, fairly straight forward for a 4 digit integer within a certain range.

    If you still would rather set an official added query var, you must white list it through the “query_vars” filter. Even so, WP will not have any idea what to do with this. You need to translate it into proper WP_Query arguments. Do so in the “pre_get_posts” action. All post queries pass through here, so be sure you only alter applicable queries. Depending on where the years are stored, you probably will be composing a meta_query argument.

    Assuming the years are currently in postmeta, once you have a large number of vehicles, you will likely find meta queries taking a very long time. For large queries involving custom data, it’s more efficient to keep the data in a custom table with each type in its own column. You can still use the normal WP_Query process by altering the SQL through the “posts_request” filter. But if efficiency is important, you’re better off querying directly through global $wpdb, then looping through the results for output.

    That part is not so bad, but you would also need to channel data destined in and out of postmeta to/from the custom table instead. I’m not clear on the scale of your project or where years are stored, it could be meta queries will be just fine, or maybe you’ve already implemented an efficient data schema.

    Hi,

    If you still would rather set an official added query var, you must white list it through the “query_vars” filter. Even so, WP will not have any idea what to do with this. You need to translate it into proper WP_Query arguments. Do so in the “pre_get_posts” action. All post queries pass through here, so be sure you only alter applicable queries. Depending on where the years are stored, you probably will be composing a meta_query argument.

    Yes, that’s what I was referring to when I was talking about other attempts in my first reply. I usually use the “query_vars” filter in order to manage queries in templates (front side) (also usefull to pass parameters through template parts), it works fine, but I didn’t manage to make it work on admin interfaces.

    If it’s alright to pull my query from the $_GET array, I think I’m going to stay with this solution.

    vehicles, you will likely find meta queries taking a very long time. For large queries involving custom data, it’s more efficient to keep the data in a custom table with each type in its own column. You can still use the normal WP_Query process by altering the SQL through the “posts_request” filter. But if efficiency is important, you’re better off querying directly through global $wpdb, then looping through the results for output.

    Yes. I also thought about this issue…

    I already “optimized” my queries in order to display the vehicles list for the selected year (queries args with relations, and a few template which use the query result as parameter instead of call it again). In comparison, this is a heavy treatment to retrieve only some years…

    As I said I’m not a backend developer so this sound very tricky for me to search and register available years in a independent table in database. I currently use ACF Pro to add and stock those data relative to each vehicle in my vehicle post-type.
    I’d like to learn how to do it, maybe over time, but unfortunately we have binding deadlines with this project.

    Thank you for those details,

    N.

    • This reply was modified 1 year, 3 months ago by profnox.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Query string URL parameter in wp-admin custom page ?’ is closed to new replies.