filters Archives - Justin Silver https://www.justinsilver.com/tag/filters/ Technology, Travel, and Pictures Tue, 19 Apr 2016 18:22:16 +0000 en-US hourly 1 https://wordpress.org/?v=6.0.1 https://www.justinsilver.com/wp-content/uploads/2013/06/cropped-apple-touch-icon-160x160.png filters Archives - Justin Silver https://www.justinsilver.com/tag/filters/ 32 32 ACF validate_value Filter With post_ID https://www.justinsilver.com/technology/wordpress/advanced-custom-fields-validate_value-filter-with-post_id/?utm_source=rss&utm_medium=rss&utm_campaign=advanced-custom-fields-validate_value-filter-with-post_id https://www.justinsilver.com/technology/wordpress/advanced-custom-fields-validate_value-filter-with-post_id/#comments Sun, 19 Oct 2014 21:22:18 +0000 http://justinsilver.com/?p=3769 Advanced Custom Forms Pro 5.0 is out, and it contains a major overhaul to the way that custom fields are handled. It also had a major impact on the way that third-party add-ons were...

The post ACF validate_value Filter With post_ID appeared first on Justin Silver.

]]>
AmpedSense.OptimizeAdSpot('AP'); AmpedSense.OptimizeAdSpot('IL'); AmpedSense.OptimizeAdSpot('IR');

Advanced Custom Forms Pro 5.0 is out, and it contains a major overhaul to the way that custom fields are handled. It also had a major impact on the way that third-party add-ons were written to extend ACF functionality which meant that it was considerable work to refactor my Validated Fields plugin to support the new architecture. The new architecture is definitely superior in my opinion and has some great new filters that we can leverage such as acf/validate_value and its siblings acf/validate_value/type={$field_type}acf/validate_value/name={$field_name}acf/validate_value/key={$field_key}.

Unfortunately this filter does not have the post_ID available to it, which greatly limits the range of things we can do with it. To work around this I found that by inserting a field named acf[post_ID] into the editor form – and the “acf” part is critical – it would be picked up and submitted with the rest of the form values. This meant that it would be available in the $_POST, really opening up the possibilities.

Sample Code

// use a unique value to prevent conflicts with other ACF fields
define( 'MY_ACF_FORM_VALUES', 'MY_ACF_FORM_VALUES' );

// add the post_ID to the acf[] form
function my_edit_form_after_editor( $post ){
	print( "<input type='hidden' name='acf[%1$s][post_ID]' value='%2$d'/>", 
		MY_ACF_FORM_VALUES, 
		$post->ID 
	);
}
add_action( 'edit_form_after_editor', 'my_edit_form_after_editor' );

// use the post_ID in your validation function
function my_validate_value( $valid, $value, $field, $input ) {
	$post_id = $_POST['acf'][MY_ACF_FORM_VALUES]['post_ID'];
	// more code!
	return $valid;
}
add_filter( "acf/validate_value", 'my_validate_value', 10, 4 );

The rest of the my_validate_value() will be up to you.

Shameless Plug.

Why reinvent the wheel? The above code is already included in Validated Field for ACF available in the WordPress repository.

The post ACF validate_value Filter With post_ID appeared first on Justin Silver.

]]>
https://www.justinsilver.com/technology/wordpress/advanced-custom-fields-validate_value-filter-with-post_id/feed/ 8
Sort by Featured Image in WordPress https://www.justinsilver.com/technology/wordpress/sort-featured-image-wordpress/?utm_source=rss&utm_medium=rss&utm_campaign=sort-featured-image-wordpress https://www.justinsilver.com/technology/wordpress/sort-featured-image-wordpress/#comments Fri, 15 Aug 2014 02:10:52 +0000 http://justin.ag/?p=3703 Featured Images on a WordPress page or post can make it easy to use a single image to represent the content of your page. Given that they look better, why not feature them at...

The post Sort by Featured Image in WordPress appeared first on Justin Silver.

]]>
AmpedSense.OptimizeAdSpot('AP'); AmpedSense.OptimizeAdSpot('IL'); AmpedSense.OptimizeAdSpot('IR');

Featured Images on a WordPress page or post can make it easy to use a single image to represent the content of your page. Given that they look better, why not feature them at the top of your content, while still preserving the rest of your ORDER BY? The answer is that it’s not terribly straightforward but you do have a couple of options. We can make use of the “_thumbnail_id” meta_key that is stores the attachment ID of the featured image, but as it is missing for pages and posts that don’t have a featured image they are omitted from The Loop entirely, which is not what we want.

Filters on WP_Query

The most robust way to sort your posts by featured image while not interfering with your existing ORDER BY is to plug into the filters for posts_fields_requestposts_join_request, and posts_orderby_request. The SQL that is going to be run passes through these filters in pieces, allowing us to modify it – and make sure that pages and posts without featured images are last! Since we don’t want to modify every WP_Query, presumably just some of them, we can add another argument to handle this for us – we’ll call it featured_image_sort.

class Featured_Image_Sort {
	public static function init(){
		add_filter( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) );
	}

	public static function pre_get_posts( $query ){
		if ( $query->query_vars['featured_image_sort'] ){
			add_filter( 'posts_fields_request', array( __CLASS__, 'posts_fields_request' ) );
			add_filter( 'posts_join_request', array( __CLASS__, 'posts_join_request' ) );
			add_filter( 'posts_orderby_request', array( __CLASS__, 'posts_orderby_request' ) );
		}
		return $query;
	}

	public static function posts_fields_request( $select ){
		global $wpdb;
		return "{$select}, IF ( _has_featured_image.meta_value IS NULL, 0, 1 ) AS has_featured_image ";
	}
	public static function posts_join_request( $join ){
		global $wpdb;
		return "{$join}\tLEFT JOIN {$wpdb->postmeta} AS _has_featured_image ON _has_featured_image.post_id = {$wpdb->posts}.ID and _has_featured_image.meta_key = '_thumbnail_id'";
	}
	public static function posts_orderby_request( $orderby ){
		return "has_featured_image DESC, {$orderby}";
	}
}
Featured_Image_Sort::init();

Using the new WP_Query argument

I like to keep things flexible, so if you want to add this to every query I would use a hook to pre_get_posts higher than a priority 10 to set the featured_image_sort flag. This is also an easy way to quickly test your code.

add_filter( 'pre_get_posts', 'sort_featured', 1 );
function sort_featured( $query ){
	$query->query_vars['featured_image_sort'] = true;
	return $query;
}

If you want to apply the sort to specific queries you would pass this flag as an argument to WP_Query as you would any other argument.

$args = array( 
	'orderby' => 'meta_value', 
	'meta_key' => 'some_meta_key', 
	'featured_image_sort' => true 
);
$query = new WP_Query( $args );

The post Sort by Featured Image in WordPress appeared first on Justin Silver.

]]>
https://www.justinsilver.com/technology/wordpress/sort-featured-image-wordpress/feed/ 3