This is searchable archive of our old support forums, which operated from 2012 - 2016. To find out how to get support for your current theme, please visit our support page.

Extending the Custom Query

  • Creator
    Topic
  • #14312
    Jason Bobich
    Keymaster

    When working with post grids and post lists within the framework there are ways to pass custom query strings through to the elements. For those that have knowledge of WP_Query, this allows you to drill down past the options available with post lists and post grids to query the posts in your own way — maybe by a tag, a custom post type, a custom taxonomy, etc.

    Where the custom query options exist

    You can implement custom query string from your WordPress admin through the following framework elements.

    1. Post Grid/List elements of Layout Builder
      There is an option called “Custom Query” you can use wen editing the element.
    2. Post Grid/List page template
      Add a custom field called “query”
    3. Post Grid/List shortcode
      Add the parameter “query” to the shortcode.
      Ex: [post_grid query=”tag=whatever&cat=1″]

    Limitations

    Now the problem with this system is that it can only take you so far. If you’re trying to do more advanced queries, this is going to require actually setting up PHP arrays, and this won’t be possible through these basic custom query options which only accept a string like:

    foo=bar&foo2=bar2&foo3=bar3

    Extending the custom query

    So, what if you need to do a more complicated query that requires PHP?

    Internally, there is a filter on the query string every time a post grid or list is generated. But there’s no way to really identify this filter with a specific post grid/list instance.

    As a work around for this, in your instance of the post grid or list, you could use the “custom query” option to pass in some unique identifier to use as a “catch” within your PHP function that you’re going to attach to the filter for all post grid and post list queries.

    So, for example, setup a post grid/list and for the custom query option write something like:

    my_unique_key

    Now from your Child theme’s functions.php you can filter onto the query argument array for all post grids/lists and use that identifier to trigger some custom query string.

    /**
     * Custom query for post lists/post grids
     */
    function my_custom_posts_query( $query, $args ){
    	if( $args['query'] == 'my_unique_key' ){
    		$query = array( 
    			// your custom query arguments
    		);
    	}
    	return $query;
    }
    add_filter( 'themeblvd_posts_args', 'my_custom_posts_query', 10, 2 );

The forum ‘The Arcadian Responsive WordPress Theme’ is closed to new topics and replies.