Support » Plugin: Query Wrangler » Add Filter

  • Hey,

    I would like to create another filter. My current project requires that I display posts that are only visible to the author. Rather than over-complicating things, I would prefer to have less plugins and make it easier long term. So adding another option for a “current user” filter to the author dropdown would be ideal. Can you help point me in the right direction? Thanks.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Jonathan Daggerhart

    (@daggerhart)

    Hi @henderson7,

    There are a couple of ways to go about creating a new filter, just depends on how complicated you want to get with it.

    1. Simple new filter with no options
    When this is added to a query this will automatically add the parameter “author = current user ID”.

    
    <?php
    // Register new filter with Query Wrangler.
    add_filter( 'qw_filters', 'qw_filter_author_is_current' );
    
    function qw_filter_author_is_current( $filters ) {
      $filters['author_is_current'] = array(
        'title' => 'Author is current user',
        'description' => 'Filter posts by current user as the post author',
        'query_args_callback' => 'qw_generate_query_args_author_is_current',
        'query_display_types' => array( 'page', 'widget' ),
      );
      return $filters;
    }
    
    // Modify the query arguments before the query is executed.
    function qw_generate_query_args_author_is_current( &$args, $filter ) {
      $args['author'] = get_current_user_id();
    }
    

    2. Slightly more complicated, a filter with options

    This version let’s you choose the between “Is current user” and “Is Not current user”.

    
    <?php
    // Register new filter with Query Wrangler.
    add_filter( 'qw_filters', 'qw_filter_author_is_current' );
    
    function qw_filter_author_is_current( $filters ) {
      $filters['author_is_current'] = array(
        'title' => 'Author is current user',
        'description' => 'Filter posts by current user as the post author',
        'query_args_callback' => 'qw_generate_query_args_author_is_current',
        'query_display_types' => array( 'page', 'widget' ),
        'form_callback' => 'qw_filter_author_is_current_form',
      );
      return $filters;
    }
    
    // Provide an option form for the filter.
    function qw_filter_author_is_current_form( $filter ) {
      if ( empty( $filter['values']['operator'] ) ) {
        $filter['values']['operator'] = 'author__in';
      }
      ?>
      <strong>Operator:</strong>
      <select
        class="qw-field-value  qw-js-title"
        name="<?php print $filter['form_prefix']; ?>[operator]">
        <option value="author__in" <?php selected( $filter['values']['operator'], 'author__in' ) ?>>
          Author is current user
        </option>
        <option value="author__not_in" <?php selected( $filter['values']['operator'], 'author__not_in' ) ?>>
          Author is NOT current user
        </option>
      </select>
      <?php
    }
    
    // Modify the query arguments before the query is executed.
    function qw_generate_query_args_author_is_current( &$args, $filter ) {
      if ( empty( $filter['values']['operator'] ) ) {
        $filter['values']['operator'] = 'author__in';
      }
      $args[ $filter['values']['operator'] ] = [ get_current_user_id() ];
    }
    

    3. Use the existing “Callback” filter, and provide it with a simple function.

    This is the simplest way to go, but is a little more brittle than providing an actual new QW filter.

    
    function my_custom_callback_author_is_current( $args, $filter ) {
      $args['author'] = get_current_user_id();
      return $args;
    }
    

    —-

    Hope this helps. I’m also happy to add something like this to the plugin if you’d like. Depending on your level of comfort, you’re welcome to submit a pull request to the GitHub repo (https://github.com/daggerhart/query-wrangler), or I can create it.

    – Another filter example: https://github.com/daggerhart/query-wrangler/blob/master/docs/examples/filter.md

    Thread Starter henderson7

    (@henderson7)

    Thanks for the detailed response. I will give them a try but I actually like the idea of the is/isn’t author option. This would be great to have in the next update.

    Cheers.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add Filter’ is closed to new replies.