• Resolved fendervr

    (@fendervr)


    Hi all, I am creating an archive page on a custom post type, showing a list of works with title, description, author and thumbnail (fetured image), basically.

    I had no problem and the archive page was successfully created.

    MY only need was that the works displayed were not ordered by a custom fields “number” but by publishing date as default.
    So I put this functions in my function.php (child theme).

    function cambia_ordine( $query ) {
        if ( $query->is_main_query() && !is_admin() ) {
            if ($query->is_post_type_archive('opera') ) {
                $query->set('orderby', 'meta_value_num');  
                $query->set('meta_key', 'numero');  
                $query->set('order', 'ASC'); 
            }       
        }
    } 
    add_action( 'pre_get_posts', 'cambia_ordine' );

    Hence the posts order was right, but all the thumbnails disappeared. If I delete the function “cambia_ordine”, thumbnails are visible again and obviously the order is wrong.

    Could you please tell me why this happen? and how can I get the right order and thumnbnails displayed using pre_get_posts?

    Many thanks in advance.
    Claudio

    • This topic was modified 4 years, 10 months ago by fendervr.
    • This topic was modified 4 years, 10 months ago by fendervr.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    is_post_type_archive('opera') would apply to any query on the page, including those for thumbnails. As the meta key argument would not apply to thumbs, nothing is returned. Instead get the “post_type” query var and conditionally set values based upon that.

    Thread Starter fendervr

    (@fendervr)

    Hi bcworkz, many thanks for your quick reply.
    I worked for years with PHP only and thught that with wordpress would be easy. But I understand that it’s not so. I have to study more and learn practically a new language. I saw what you suggested (I believe), but it’s not simple to put it in practice to me without a real understanding.

    As the meta key argument would not apply to thumbs, nothing is returned.

    The meta key argument you mention here is the ‘opera’ within is_post_type_archive('opera')?

    Anyway I solved changing my function as follow:

    function cambia_ordine( $query ) {
    	if ( !is_admin() && $query->is_main_query()){
    		$query->set('post_type','opera');
    		$query->set('orderby', 'meta_value_num');  
    		$query->set('meta_key', 'numero');  
    		$query->set('order', 'ASC'); 
    	}
    return $query;
    }

    and it works. Is this the correct way you intended?
    Many thanks again. Bye

    Moderator bcworkz

    (@bcworkz)

    I’m surprised that works in all cases, I think you may not have checked other queries for your site yet. I was thinking more like:

    if ( !is_admin() && $query->is_main_query()){
      if ('opera' == $query->get('post_type')) {
        $query->set('orderby', 'meta_value_num');
        $query->set('meta_key', 'numero');  
        $query->set('order', 'ASC'); 
      }
    }
    • This reply was modified 4 years, 10 months ago by bcworkz.
    Thread Starter fendervr

    (@fendervr)

    I think you are right. It is possible. I was focalized myself on the lack of images in a lot of my posts (55 in total) probably not checking other query.
    Wordpress is quite easy but it’s necessary to understand well how fit the many function and query among themselves, f.
    Thanks for your patience and help. I resolved at least for now.
    Bye
    Claudio

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Thumbnails not displayed after pre_get_posts query’ is closed to new replies.