Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
d.merlitti

Generate a filtering url programmatically

When a user adds a filter by using the user interface available in post.php, the plugin calculates a url containing several “ac_” parameters.
Example:

edit.php?ac-actions-form=1&s&post_status=all&post_type=documento&m=0&layout=65fd5c2148633&filter_action=Filtra&action=-1&paged=1&action2=-1&ac-rules=%7B"condition"%3A"AND"%2C"rules"%3A%5B%7B"uid"%3A"10a59e4e200000"%2C"id"%3A"b3fac274a7efa0"%2C"operator"%3A"equal"%2C"value"%3A"28"%7D%5D%7D

Is is possible to calculate these URLs programmatically so that they can be used for example in other custom administration pages?

1 month ago
d.merlitti

In my specific case I found a solution based on this php code:

$acRules = urlencode("{\"condition\":\"AND\",\"rules\":[{\"uid\":\"10a59e4e200000\",\"id\":\"b3fac274a7efa0\",\"operator\":\"equal\",\"value\":\"{$term->term_id}\"}]}");
      $acParams = [
        'post_type' => 'documento',
        'ac-actions-form' => 1, 
        'layout' => '65fd5c2148633',
        'filter_action' => 'Filtra',
        'action'=> -1,
        'paged' => 1,
        'action2'=> -1,
        'ac-rules' => $acRules];            
      $filterUrl = add_query_arg($acParams, admin_url('edit.php'));

but it is clear that the ‘layout’ parameter should be retrieved in an installation-dependent manner…

1 month ago
Stefan van den Dungen Gronovius
Developer

Using our Saved Filter feature can be helpful for your use case. You can learn more about it here.

https://docs.admincolumns.com/article/73-how-to-use-saved-filters

Since filtering depends on the columns in a table, you’ll need to know the layout ID you’re working with. Saved Filters allow you to save your filter settings, and you can even set them as the default for a specific table using Predefined Filters. Once this is done, you only need to know the Layout ID to use in your custom link.

In any case, you must always know the specific layout ID to apply filter parameters.

1 month ago
d.merlitti

Dear Stefan,

thank you for your suggestions.
In my case I’m trying to do everything in a programmatically way.
The layout ID and the field ID can be read from the table wp_admin_columns if you know the post_type and the field name that you want to filter on. Example:

$postType = 'contenuto';
$fieldName = 'tipologia';
$layout = $wpdb->get_row("select list_id,columns from {$wpdb->prefix}admin_columns where list_key='$postType'");
$layoutId = $layout->list_id;
$columns = unserialize($layout->columns);
$arr = array_filter($columns, function($col) { 
  return (array_key_exists('pods_field', $col) && ($col['pods_field'] == $fieldName));
});
$fieldId = $arr ? $arr[array_keys($arr)[0]]['name'] : null;
$uid = uniqid();
$acRules = urlencode("{\"condition\":\"AND\",\"rules\":[{\"uid\":\"$uid\",\"id\":\"$tipologiaDocumentoFieldId\",\"operator\":\"equal\",\"value\":\"{$term->term_id}\"}]}");
$acParams = [
        'post_type' => $postType,
        'ac-actions-form' => 1, 
        'layout' => $ayoutId,
        'filter_action' => 'Filtra',
        'action'=> -1,
        'paged' => 1,
        'action2'=> -1,
        'ac-rules' => $acRules];            
$filterUrl = add_query_arg($acParams, admin_url('edit.php'));

In this way I can calculate the url for filtering all posts of type $postType on field $fieldName by AC.

1 month ago
Stefan van den Dungen Gronovius
Developer

I see, there are no real way to do this easily within our plugin, so writing your own SQL to do that is a valid workaround in this case. Just be sure, I expect you to have a working solution now. Do you still need our input from us or can I close this ticket?

4 weeks, 1 day ago
d.merlitti

Dear Stefan,

I have a working solution. You can close the ticket, thanks.

4 weeks, 1 day ago

You must be logged in to reply to this topic.