Replacing a query string parameter value

Hello

I have this rewrite rule in my init action:

add_rewrite_rule(
    '^gallery/([^/]*)/([^/]*)$',
    'index.php?category_name=$matches[1]&connected_type=vendors_posts&connected_items=$matches[2]',
    'top'
  );

connected_items var is currently the user ID, so all my urls are something like: /gallery/classics/1 and this is displaying all posts inside classics category connected with the user with ID 1, the query string for the plugin should be numeric to make it work... so I would like to have: /gallery/classics/rembrandt but before to query for posts get the ID for user rembrandt and replace the query string var value, so I keep a nice url and the thing remains working as it is right now

Please help faster, I can not wait a day for this, I am paying for support.

Regards

Adrian

  • Hoang Ngo

    Hi @adrian,

    I hope you are well today.

    Instead of using user id, how about the user_login, so this will be unique and human readable.

    And later, we can use the action pre_get_posts, find the user id from user login, set it to the query ?

    Example of how to use the "pre_get_posts" hook

    function add_user_id_to_the_query( $query ) {
    //your logic
    }
    add_action( 'pre_get_posts', 'add_user_id_to_the_query' );

    Best Regards,
    Hoang

  • adrian

    Hello @Hoang Ngo

    I am using wp-posts-to-posts plugin to connect posts, here are the details for its query vars:

    https://github.com/scribu/wp-posts-to-posts/wiki/Query-vars

    The query var needs the id number the code is the one I sent in the first post

    I guess I just need the way to replace the query var with the ID before the query is executed but I dont know how to do this

    Please help with some example

    Regards

    Adrian

  • Hoang Ngo

    Hi @adrian,

    I found this example in the github

    $connected = get_posts( array(
      'connected_type' => 'posts_to_pages',
      'connected_items' => get_queried_object(),
      'nopaging' => true,
      'suppress_filters' => false
    ) );

    So if you are doing something similar, we can modify like this.

    //assume that you already have the $user_login
    $author = get_user_by('login',$user_login);
    $connected = get_posts( array(
      'connected_type' => 'posts_to_pages',
      'connected_items' => $author,
      'nopaging' => true,
      'suppress_filters' => false
    ) );

    This will get the posts connected with the author, and still keep your nice url.

    Best Regards,
    Hoang

  • adrian

    Hello

    I think you are getting too complex about my question lets start again:

    This url works as expected:

    index.php?category_name=mycategory&connected_type=vendors_posts&connected_items=1

    And this is currently being used by my rewrite rule:

    /gallery/mycategory/1

    I just want to change the last parameter to /gallery/mycategory/oneuser

    but in the middle of the process before the query for the archive get executed I need to replace oneuser parameter for its id so I keep a nice url and the archive keeps working as it is right now

    Regards

    Adrian

  • Hoang Ngo

    Hi @Adrian,

    Thanks for your information, I understand the flow. But for correctly inject the params, I will need more information.

    Firstly, how you get the posts, using normal wordpress query or through the plugin.

    Second, when you do that, in a hook, or in a loop, if a loop, can you please post the code here.

    I will need the exactly the code you using to getting the source, so I can modify there. If I use a global filter, so I don't have any clue where to exclude and it will affect to all query, which is not good.

    Best Regards,
    Hoang

  • adrian

    Hello @Hoang Ngo

    Thank you for your patience

    first, I am not using any code for this page, I really mean that default wordpress archive is the one doing the filter using the url that I described above, I am just uing the regular loop in my template, not querying the objects at any place that is just wordpress doing it

    So is working with no code from my side, I am just trying to mask it with a nice url

    Regards

    Adrian

  • Saurabh

    Hi Adrian,

    From what I understand, you have registered a connection called vendors_posts using a process here: https://github.com/scribu/wp-posts-to-posts/wiki/Basic-usage.

    If you haven't could you clarify how you have got the vendors_posts query variable?

    If you have registered it indeed, then could it be that you could change your registration process to register this connection using user names instead of user ids?

    Could you post the details of what you have done to get where you are?

    The plugin that you are using is quite complex in its working and code and it won't be possible to provide useful support without the details of your implementation. I agree with Hoang that any attempt by us without such background knowledge will be like shooting arrows in the dark.

    Best

    Saurabh

  • adrian

    Hello

    I think you are still missing the point of my question here, the connection is fine and is working as intended, also I am using it in some custom queries and is between posts and users:

    https://github.com/scribu/wp-posts-to-posts/wiki/Posts-2-Users

    I don't see a way to register the connection using the usernames instead, it just works with the users IDs, anyway for your peace of mind here is the code for the connection:

    p2p_register_connection_type( array(
            'name' => 'vendors_posts',
            'from' => 'post',
            'to' => 'user'
            'to_query_vars' => array( 'role' => 'vendor' )
        ) );

    This connection make possible to get all posts connected to a user if you go to: /index.php?connected_type=vendors_posts&connected_items=1

    Where connected_items query var is the user id, this works using the regular Wordpress archive template, so my real question IS NOT ABOUT THE CONNECTION, is about the rewrite rule that I sent at the start, I need and action in the middle to get the query var value with the username get the id for that username and replace the query var with the id so it keeps working as the real url... so I can keep it working as it is right now but with the username in the url

    Regards

    Adrian

  • Saurabh

    Hi Adrian,

    No, we did understand your problem. I had pretty much guessed how you have come to this solution that you are using. The problem is a little complicated.

    Once you replace the user id with username in your url rewrite, this is what will happen:

    1. connected_items will be assigned the username, instead of the user_id.
    2. The query, that the plugin will want to run, will fail because it is expecting a user_id.

    So, the problem is to replace the value of connected_items from the username to user_id before the query is run.

    Unfortunately, this can only be done by filtering the plugin's query.

    I'm not sure where this happens, but I feel a possible candidate is here:

    https://github.com/scribu/wp-posts-to-posts/blob/master/core/query.php#L64

    You could use the filter p2p_connected_args and see what the output array is and accordingly replace the username with user_id, using get_user_by and see if that works.

    Please contact the said plugin's support for further insight into this. I hope this gives you enough direction to get it done.

    Beyond this, it'd be really difficult to help you out unless we have direct access to your setup. If you'd like more help, please mail me access to your setup via the contact form:

    - Mark to my attention - ATTN: Saurabh Shukla
    - Link back to this thread
    - Include admin/network access
    - Include cPanel (I will need to look at the DB so need PHPMyAdmin or similar)
    - Include FTP (so I can temporarily modify the plugin to look for an appropriate filter)
    - Include any relevant URLS for your site (so, I can directly look at the pages, without searching)

    On the contact form, select "I have a different question", this ensures it comes through and gets assigned to me.

    Best

    Saurabh

  • adrian

    Well

    couldnt solve it with current filters, the one you were suggesting it allows me to add some stuff for the query string byt the item connected couldnt be altered as it is just filtering the query array, the item var was the one needed... so I figured out debugging the plugin that was not possible to alter that var, so I decided to add my own filter inside plugin to accomplish this...

    I am getting in touch with the plugin creator to propose this change and make a pull request if he consider this.

    Anyway thanks for your time, you provide some light at the end

    Regards

    Adrian

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.