Randomize Portfolio Images Broken..help!

I’m getting this error:
Notice: Undefined index: post_type in /…/wp-content/themes/x-child/functions.php on line 47

The code that was once working that Themeco suggested is here:

/randomize portfolio images/
function cmk_custom_order( $wp_query ) {
/* Get the post type from the query*/
$post_type = $wp_query->query[‘post_type’];
/If Post type is Portfolio/
if ( $post_type == ‘x-portfolio’) {
/* Set Order to Random*/
$wp_query->set(‘orderby’, ‘rand’);
/* un-comment if not using random order /
//$wp_query->set(‘order’, ‘DESC’);
}
}
/
Change Order settings before displaying the Postype*/
add_filter(‘pre_get_posts’, ‘cmk_custom_order’);

The page it’s used on is here:

Please help!

Hi There,

Thanks for writing in! Can you please try updating your code as follows and see if that helps.

/*randomize portfolio images*/
function cmk_custom_order( $wp_query ) {
     /* Get the post type from the query*/
    if (isset($wp_query->query['post_type'])) {
    	$post_type = $wp_query->query['post_type'];

    	/*If Post type is Portfolio*/
    	if ( $post_type == 'x-portfolio') {
   		/* Set Order to Random*/
		$wp_query->set('orderby', 'rand');
    	/* un-comment if not using random order */
    	//$wp_query->set('order', 'DESC');
    	}
    }
}
/* Change Order settings before displaying the Postype*/
add_filter('pre_get_posts', 'cmk_custom_order');

Hope that helps.

1 Like

Brilliant. That also fixed the ginormous rev slider images that appeared on my homepage as well.
Thanks so much!

You’re always welcome!

Cheers.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.