Divi WordPress Theme
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

WordPress fix Reply To: Get images based on TAG

Discussion in 'Misc WordPress Requests' started by bcworkz, Jan 16, 2018.

  1. bcworkz


    Reply To: Get images based on TAG, by bcworkz

    With only cut and paste coding skills, your vision could be a big ask. But you seem to have a lot of perseverance, so you may just be able to pull it all together anyway. You’re on the right track. WP_Query is at the heart of getting anything post related.

    BTW, while getting image data out of content could work, it’s horribly inefficient. You generally would want to get image data from related attachment posts. The problem with attachments though is they are only attached to the post in which they were first used. Posts with reused images do not have associated attachments. To get at reused images, you really do have to parse post content.

    Back to WP_Query. Unless you are dealing with direct user input of terms, you are generally better off working with IDs instead of term slugs. The tax_query argument supports all kinds of criteria. Look through WP_Query docs to learn how to query for taxonomy term IDs instead of term slugs.

    Another problem with using found code is you cannot know how closely it applies to your installation. For example, there is no ‘attachment_category’ by default in WP. Unless you or a plugin created it, it’s not going to work. The taxonomy for default tags is ‘post_tag’.

    As you recognized, you need to somehow dynamically get tag IDs from the image clicked. The way to do that is build the IDs into the link href when you output the images. The link would lead to that custom page I talked about. That page would have something like the query you found, adjusted as mentioned above.

    Maybe this page is called “Related Images”, its slug is related-images. The link’s href might be something like example.com/related-images/?post_tag=123 if 123 is the tag ID of the image to which this link is associated. On this page template with your new found WP_Query, the passed ID is available from $_GET['post_tag']. Making a custom page template is the key to getting this code to work the way you want. Once you have a template, add a new page that uses it. When you request this page, your code will run.

    We can worry about how to generate links to this page later. You should focus on only one part of this at a time or you will drive yourself insane! For now you could just hardcode such a link on a plain HTML page as a stand in until you get this query and page template working.

    With that code, adjusted as described, you end up with $query_images as a query object containing a list of attachment posts. You can run a normal custom query loop that’s normally used for posts to get to each attachment post in turn. With the attachment post, you can get the related img element with wp_get_attachment_image(). Besides the attachment post ID, the function accepts a size argument and an argument for what attributes to include in the img tag, such as class and ID and alt values.

    Simply echo out the returned value. When you do that for every attachment in the query, you’ll have all the images with the particular tag ID all on one page.

    Don’t try to cobble something together all at once and hope it works. Debugging code assembled like that is difficult to debug. Only do a little at a time, testing frequently. You can usually check the value of any variable with print_r( $variable_to_check ); When you check an object like $query_images this way, you will get a lot of output. It helps if you output this inside of <pre> </pre> tags so the output is organized. Otherwise the whitespace is stripped and it all runs together.

    A lot of the output will have little meaning to you. What you are looking for is an array of attachment post data. There should be one for every image that has the particular tag assigned. If you don’t get that, check your query arguments. In particular $_GET['post_tag'] and $query_images_args.

    That should keep you busy for a while I hope you get a little rush of achievement after successfully completing even a minor bit of code. If you find that rewarding, you’ll do OK. If that’s the case, I can sincerely wish you to have fun!

    Reply To: Get images based on TAG

Share This Page

Monarch Social Sharing Plugin