• Resolved alexislevrai

    (@alexislevrai)


    Hello,

    I’m have a post Query with tax_query, both are custom post type and cutom taxonomy.
    And it looks like the tax_query brake the attachement size of the post.
    My wp_get_attachment_image load in full size, and every time I remove my tax_query wp_get_attachment_image load as it should be.

    My post query

    $query_args = array(
    'post_type'      => 'knowledge',
    'posts_per_page' => 99,
    'tax_query'      => array(
        array(
            'taxonomy' => 'knowledge_category',
            'field'    => 'term_id',
            'terms'    => 316,
        ),
    ),
    );

    And my attachement is like this but I’m 99,99% sur there is no issue there as it work when I remove the tax_query part from my post query.

    $post_thumbnail_id = get_post_thumbnail_id(); 
    $title = get_the_title($post_thumbnail_id);
    echo wp_get_attachment_image( $post_thumbnail_id, 'small', false, array('title'=> $title, 'alt'=> $title, 'loading'=> 'lazy', ) );

    Any idea from what it can come ?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s not clear what you’re doing with $query_args. But it’s clear it’s inappropriately influencing the attachment query. You need to do something to limit the application of $query_args. What that would be depends on how you use $query_args in the first place. Please post the code that makes use of $query_args so the surrounding context can be seen.

    Thread Starter alexislevrai

    (@alexislevrai)

    The thing is that I’m trying to display all the post from a custom post type bu category, for all categories.

    Here is the full code, I don’t really see how can I do that to an other way …

    $knowledge_categories = get_terms('knowledge_category');
    foreach ($knowledge_categories as $category) {
    	$category_url = get_term_link($category);?>
    		<header><?php echo esc_html($category->name); ?></header>
    		<?php 
    		$query_args = array(
    			'post_type'      => 'knowledge',
    			'posts_per_page' => 99,
    			'tax_query'      => array(
    				array(
    					'taxonomy' => 'knowledge_category',
    					'field'    => 'term_id',
    					'terms'    => $category->term_id,
    				),
    			),
    		);
    		
    		$knowledge_query = new WP_Query($query_args);
    		if ($knowledge_query->have_posts()) :
    			while ($knowledge_query->have_posts()) : $knowledge_query->the_post(); ?>
    				<?php get_template_part( 'card-post-and-page' );?>
    			<?php endwhile;
    			wp_reset_postdata();
    		endif;
    }

    • This reply was modified 1 year, 1 month ago by alexislevrai.
    Moderator bcworkz

    (@bcworkz)

    Ah! Appears to be a custom query on a template. Good to know, thanks. You’ve correctly used wp_reset_postdata(), so it’s difficult to fathom how adding a “tax_query” arg could possibly influence the wp_get_attachment_image() call. It sounds like the call is returning the right image, only it’s the wrong size? An image’s size has nothing to do with WP_Query, so it’s an even greater mystery.

    Are you sure the small file size actually exists and is accessible? If it cannot be found or cannot be accessed, I think the code would fall back to the full size.

    The only other thing I can imagine is there is some other code filtering the wp_get_attachment_image() call that is indirectly influenced by which posts are returned by WP_Query. As a test to eliminate possible sources, try deactivating all plugins. If the problem persists, it must be something about your theme. If this resolves the issue, restore your plugins, one at a time, until the issue returns.

    Once you’ve identified the responsible module, search its source code for add_filter() calls hooking to anything image related. One of these is likely the culprit.

    The other thing you could do to investigate is to modify the wp_get_attachment_image() source code as though you were debugging it to determine exactly where the requested size is ignored and the full size is used instead. We’re not supposed to modify core code, but these are just temporary changes. You’re not going to keep them in place. Thus, keep a backup of the original file before editing so it’s easy to revert after your investigation.

    Thread Starter alexislevrai

    (@alexislevrai)

    Glad to read that I’m not doing it wrong.

    It sounds like the call is returning the right image, only it’s the wrong size? An image’s size has nothing to do with WP_Query, so it’s an even greater mystery.
    Are you sure the small file size actually exists and is accessible? If it cannot be found or cannot be accessed, I think the code would fall back to the full size.

    Yes that’s right I have the right image but the full size and without src-set, and I’m 100% sure all the size exists, if double/triple check !

    And the strange part is that as soon I remove

    'tax_query'      => array(
    				array(
    					'taxonomy' => 'knowledge_category',
    					'field'    => 'term_id',
    					'terms'    => $category->term_id,
    				),
    			),

    Image work like expected …?

    Thank you for your feedback, I’m gonna try to do all your step you mention and I will let you know asap.

    Thread Starter alexislevrai

    (@alexislevrai)

    I finally figure out the issue.
    Wasn’t my code, wasn’t a plugin overriding code, wasn’t missing images. And I don’t really know what cause this problem.

    So I’m using Instant Image plugin, a plugin that offer one click image uploads directly from media library like Unsplash, Openverse, Pixabay and Pexels.
    When I’ve changed my theme I used Regenerate Thumbnails (from Viper007bond) to bulk resize all my image to fit my new theme. And for some reason, images originate from Instant Image have been correctly resized but not “linked” to the featured image.

    Problem only appear for featured image.
    I’ve manually removed and res-select each featured image issu from Instant Image library and it work.

    Really strange, don’t really find what was the problem, but found a solution.

    Anyway, thank you for your help!

    Moderator bcworkz

    (@bcworkz)

    I was pretty sure it wasn’t your code, but I couldn’t fathom how it could cause a problem by its presence. My best guess is the thumbnail regeneration process somehow corrupted the attachment post data so that changing the query the way you did caused getting the right size image to fail.

    At least it’s solved!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Tax_query brake attachment size and srcset’ is closed to new replies.