• Resolved disinfor

    (@disinfor)


    I’ve searched everywhere and can’t quite wrap my head around a particular issue I’m working on.

    What I’m trying to do is get all the images belonging to a particular custom post type and category. I’ve got it worked out already where it returns the separate arrays for each post in that custom post type. However, I actually have the need to randomize the order of the images that are returned.

    Currently, the posts are returned randomly (e.g. Post 1, Post 2 and Post 3 will return and display in a random order) and the images per post are randomized as well (e.g. Post 1 images will return in a random order), but they are still grouped by post (Post 1 images do not get intermixed with Post 2 images and so forth).

    What I need is it to be a random order where it displays images like:
    Post 1 – Image 2
    Post 2 – Image 3
    Post 1 – Image 1
    Post 3 – Image 4
    Post 2 – Image 5

    https://pastebin.com/0VsfYBFW

    In the pastebin is what I currently am using. I also have another version using a custom WP_Query, but I get the same results, as the foreach() will output an array for the first post, then an array for the second post, etc.

    Is there a way to merge the arrays returned from get_posts() and then work through that with a foreach()? Or am I missing something completely simple.

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

    (@bcworkz)

    The best solution would be to separately query for all image attachments whose parents meet your criteria, orderby rand. You then have two big random arrays, one of posts, one of images. Then step through the arrays by index number using a counter that is incremented for each iteration of the loop, outputting a post and image each time.

    The problem is I’m not sure you can construct such a query with WP_Query. You could do this using $wpdb methods, but I suck at writing SQL queries, so I can’t help you with that part ??

    Alternately, you could work off what you already have. What would need to change is to not send output from the loop you have. The loop would simply accumulate all the images. To accumulate images, just add square brackets to $thumb, like so:
    $thumb[] = wp_get_attachment_image($image->ID, 'thumb', false );

    After the main post loop runs, randomize $thumb with shuffle($thumb); You now have two big random arrays just like you would have with the separate image query. Run another loop to output the content using an index counter the same way as previously mentioned.

    Thread Starter disinfor

    (@disinfor)

    Thanks bcworkz! I ended up using $wpdb methods. I’m terrible at SQL queries as well, but I got the shuffle to work. I’m now trying to get it to only return from the custom category. Once I get that part of the query, I’ll post the working code. This is the post that helped me get to where I currently am.

    Thread Starter disinfor

    (@disinfor)

    OK! Got it to work with some help from a friend who is skilled in SQL queries. Here’s the working code:

    This runs at the top of the page to get the array values we will use in the SQL query:

    <?php
    				$catargs = array('taxonomy'=> 'custom-category');
    				$categories = get_the_terms($post->ID, $catargs); // Global variable for terms
    		?>

    Here’s the magic query:

    <?php
    
    			$post_cat = $category->taxonomy; // This will equal a custom category type.
    			$post_term = $category->term_id; // This will equal the page's category id.
    				// Sets the global $wpdb
    				global $wpdb;
    				// Starts the db query
    				$query = "
    				SELECT DISTINCT ID, guid, post_parent FROM $wpdb->term_taxonomy, $wpdb->terms, $wpdb->posts p where p.post_type = 'attachment'
                                    AND p.post_mime_type LIKE 'image/%'
                                    AND p.post_status = 'inherit'
                                    AND p.post_parent IN
                                            (SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = $post_term)
                                    AND $wpdb->term_taxonomy.taxonomy = '$post_cat'
                                    AND $wpdb->terms.term_id = $post_term
    				";
    
    			$results =  $wpdb->get_results( $query );
    
    			if ( $results ) {
    			shuffle($results);
      					foreach ( (array) $results as $image ) {
      						$url = get_attachment_link($image->post_parent);
      						$thumb = wp_get_attachment_image( $image->ID, 'thumbnail', false );
      						$alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true);
      						echo '<li style="display:inline-block;"><a href="' . get_permalink($image->post_parent) .'">' . $thumb . '</a></li>'."\n";
      					}
    				}
    		?>

    What this will do is return all images associated with a particular custom category ID.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Merging get_posts returned arrays’ is closed to new replies.