Soliloquy Documentation

Documentation, Reference Materials, and Tutorials for Soliloquy

Featured Content: Custom Field Queries

Important: This doc assumes you’re comfortable creating and/or editing PHP files in your WordPress installation and is intended for advanced users.

Note: This document is relevant only if you have a Developer or Master license and you’re using the Featured Content Addon.

By default, custom meta field queries are not part of the Featured Content Addon interface. However, that doesn’t mean that you can’t do them. On the contrary, it is quite easy to do with the powerful soliloquy_fc_query_args filter.

The main argument for the filter, $query_args, is an array of parameters that are used to find matching content.  This array of parameters is formatted for WP_Query – so you can use anything you see at https://codex.wordpress.org/Class_Reference/WP_Query.

Examples


Filter Featured Content by Author

Step 1 – Create Plugin File

The first step is to create a new php file in the wp-content/plugins/ directory of your WordPress installation. The easiest way to do this is using your favorite FTP program or through your web hosting account’s file editor (if they provide one).

Once you’ve navigated to the wp-content/plugins/ directory, create a new file with the name soliloquy-featured-content-custom-queries-author.php

Step 2 – Edit the New Plugin File

Next, you’ll want to copy and paste the following code into the soliloquy-featured-content-custom-queries-author.php plugin file you’ve created:

<?php
/**
* Plugin Name: Soliloquy - Featured Content - Custom Queries - Author
* Plugin URI: https://soliloquywp.com
* Version: 1.0
* Author: Tim Carr
* Author URI: http://www.n7studios.co.uk
* Description: Restrict Featured Content by Author
*/

/**
* Add Author Arguments to Featured Content Queries
*
* @param string $html HTML Link
* @param obj $post WP_Post
* @param array $data Slider Configuration
* @return string HTML Link
*/
function sol_soliloquy_fc_custom_author( $query_args, $id, $data ) {

	// You could check for a specific slider slug here, if you only want
	// to amend your FC query on a single slider.
	if ( $data['config']['slug'] != 'my-slug' ){
		return $query_args;
 	}
	// $query_args is an array for WP_Query:
	// http://codex.wordpress.org/Class_Reference/WP_Query
	$query_args['author__in'] = array( 1, 2 ); // Only get Posts that belong to User IDs 1 or 2

	// Return query arguments
	return $query_args;

}
add_filter( 'soliloquy_fc_query_args', 'sol_soliloquy_fc_custom_author', 10, 3 );

Step 3 – Activate the New Plugin

The last step is to navigate to the WordPress Admin > Plugins screen, locate the plugin named Soliloquy – Featured Content – Custom Queries – Author and activate the plugin.

That’s it! You’ve successfully filtered your Featured Content slides by Author!


Filter Featured Content by Custom Field

Step 1 – Create Plugin File

The first step is to create a new php file in the wp-content/plugins/ directory of your WordPress installation. The easiest way to do this is using your favorite FTP program or through your web hosting account’s file editor (if they provide one).

Once you’ve navigated to the wp-content/plugins/ directory, create a new file with the name soliloquy-featured-content-custom-queries-custom-fields.php

Step 2 – Edit the New Plugin File

Next, you’ll want to copy and paste the following code into the soliloquy-featured-content-custom-queries-custom-fields.php plugin file you’ve created:

<?php
/**
* Plugin Name: Soliloquy - Featured Content - Custom Queries - Custom Fields
* Plugin URI: https://soliloquywp.com
* Version: 1.0
* Author: Tim Carr
* Author URI: http://www.n7studios.co.uk
* Description: Restrict Featured Content by Author
*/

/**
* Add Custom Field Arguments to Featured Content Queries
*
* @param string $html HTML Link
* @param obj $post WP_Post
* @param array $data Slider Configuration
* @return string HTML Link
*/
function sol_soliloquy_fc_custom_fields( $query_args, $id, $data ) {

	// You could check for a specific slider ID here, if you only want
	// to amend your FC query on a single slider.
	if ( 29 !== $id ) {
		return $query_args;
	}

	// $query_args is an array for WP_Query:
	// http://codex.wordpress.org/Class_Reference/WP_Query

	// Check if Featured Content has already set some Custom Field arguments
	// If not, set the meta (custom field) query as an array
	// This prevents us overwriting any existing arguments that may exist
	if ( ! is_array( $query_args['meta_query'] ) ) {
		$query_args['meta_query'] = array();
	}

	// Only get Posts that have a custom field name of 'test' equal to a value of '1'
	// See https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters for full parameters
	$query_args['meta_query'][] = array(
		'key' 	=> 'test',
		'value' => '1',
	);

	// Return query arguments
	return $query_args;

}
add_filter( 'soliloquy_fc_query_args', 'sol_soliloquy_fc_custom_fields', 10, 3 );

Step 3 – Edit the Plugin File

This example code is not plug-and-play so you’ll need to be sure and edit the code to fit your specific needs.

Step 4 – Activate the New Plugin

The last step is to navigate to the WordPress Admin > Plugins screen, locate the plugin named Soliloquy – Featured Content – Custom Queries – Custom Fields and activate the plugin.

That’s it! You’ve successfully filtered your Featured Content slides by Custom Field!


Troubleshooting

  • Be sure you’re running Soliloquy 2.x or higher
  • You may need to include the following code to prevent your Featured Content slider from being cached; update ID in the snippet with the ID of your Featured Content slider:
    add_filter( 'soliloquy_fc_cache_ID', '__return_true' );
[i]
[i]