Lassie Posted February 17, 2015 Share Posted February 17, 2015 Following on from my earlier post I have a further issue and that is how to dynamically create the varaibles to pass to the shortcode. My code need to take the post ids which are retrieved from the database and put them in the gallery short code. At present this works $image1="499"; $image2="496"; //$images= $image1.$image2; // echo do_shortcode('[gallery include =' . $image1 .']'); echo do_shortcode("[gallery include={$image1},{$image2}]"); What I want to do is create the variables which maybe multiple depending on the post ids returned from the query. The query and associated code is $attachments_id=array(); global $wpdb; $result = mysql_query("SELECT post_id FROM sw_images_upload WHERE property_title = '$property_title' ");// Get the post IDs while ( $row = mysql_fetch_assoc( $result ) ) { $attachments_id[]=$row['post_id'];//put all post ids for specific address into array } foreach ($attachments_id as $iPostID) { //check we have post ids echo $iPostID; }//end of foreach //pass post ids to gallery shortcode //problem is how to create $image1 etc and put it in the shortcode? echo do_shortcode("[gallery include={$image1},{$image2}]"); Quote Link to comment Share on other sites More sharing options...
grissom Posted February 17, 2015 Share Posted February 17, 2015 (edited) You could try building up a string like this : you will end up with a string you can then insert into your shortcode statement $string = ''; while ( $row = mysql_fetch_assoc( $result ) ) { $string .= $row['post_id'].','; } // take off the last comma $string = substr($string, 0, -1); Edited February 17, 2015 by grissom Quote Link to comment Share on other sites More sharing options...
maxxd Posted February 17, 2015 Share Posted February 17, 2015 (edited) Couple things right off the bat - I'm not sure where $property_title is being assigned, but unless it's escaped elsewhere you're leaving yourself open to SQL injection. Also, are you printing the post_ids as a test or are you attempting to create the string as grissom suggests? Give this a shot - $post = $wpdb->get_results($wpdb->prepare( "SELECT post_id FROM sw_images_upload WHERE property_title = %s", $property_title ), ARRAY_N); if($post){ $args = implode(',',$post); //concatenate the results into a string, each value separated by a comma } echo do_shortcode("[gallery include='{args}']"); Basically, this is using WordPress's version of a prepared statement (not a true prepared statement, but at least you're not putting $_REQUEST data directly into a query) to return a numerically indexed array of post_ids that match your property_title. implode() is base php that does exactly what grissom suggests about making the array a string. Then you inject that string into your shortcode. I don't guarantee this will work if it's just cut and pasted into the functions file, but hopefully it'll point you in the right direction. Oh - and also, please use the code button ("< >") on the editor to post code. It makes it much more readable. Edited February 17, 2015 by maxxd Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 17, 2015 Author Share Posted February 17, 2015 Hi, Thanks for that. I amended the code but dont get any output from the shotcode. echo the $string confirms it has the post ids, code so far global $wpdb; $string= ''; $result = mysql_query("SELECT post_id FROM sw_images_upload WHERE property_title = '$property_title' ");// Get the post IDs while ( $row = mysql_fetch_assoc( $result ) ) { $string .=$row['post_id'];//put all post ids for specific address into array } echo $string; // take off the last comma $string = substr($string, 0, -1); echo do_shortcode("[gallery include={$string}]"); Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 17, 2015 Author Share Posted February 17, 2015 Hi, My reply to grissom crossed with your response. Thanks. code noted. The property title is passed from a form and the post ids are just to check the posts and the associated attachments are from the right property. This piece of code is just a local test to get the basisc of what I am trying to do working. I tried your code , but again get no output. Should it be $args? Tried that but no output. Quote Link to comment Share on other sites More sharing options...
maxxd Posted February 17, 2015 Share Posted February 17, 2015 Try dumping the contents of $post after the query is run, and the value of $args after the explode() call ($args is just what I called the resulting string, btw). Make sure it's actually got all the correct information and that the information is formatted properly. Also, make sure you've got error reporting turned on. Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 17, 2015 Author Share Posted February 17, 2015 Hi Thanks for your reply. I went back to my original code and modified it similar to your suggestions, which seem to be on the right track. I have pasted the results for each stage but still cant get the shortcode to work. I will test your code again. error reporting is on and no error reported. not sure where the code button is but tried inline. $attachments_id=array();//$property_title="23 Park Road";//property title will be passed from front end form where property is selected.global $wpdb;$result = mysql_query("SELECT post_id FROM sw_images_upload WHERE property_title = '$property_title' ");// Get the post IDswhile ( $row = mysql_fetch_assoc( $result ) ) {$attachments_id[]=$row['post_id'];//put all post ids for specific address into array}foreach ($attachments_id as $iPostID) {//check we have post idsecho $iPostID;//result 46649449$args[]=$iPostID;}//end of foreach$images=implode(" , ",$args);echo $images;//result 466 , 494 , 497echo do_shortcode("[gallery include={$images}]");print_r($args);// result Array ( [0] => 466 [1] => 494 [2] => 497 )// format from codex [gallery include=23,39,45] Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 17, 2015 Author Share Posted February 17, 2015 Hi All, I think I have solved the problem thanks to your help. The last code I pasted does work and renders the gallery. My sql pulled the post ids and having got those I needed to pull the attachment ids. I will clen up the code and post it a little later on as I have engagements. Thanks again. Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 18, 2015 Author Share Posted February 18, 2015 Hi All, I promised to post my solution to my problem, which is below. Please note that I am building this on wordpress and wp-pluck is a wordpress function This takes an address from a gravity form, selects all the posts for that address with image attachments puts the ids of the attachments into the gallery shortcode and displays it. //get post id from form//select all post ids for a specific address$property_title=$_GET['property'];global $posts;//$wpdb->show_errors();//debug$attachments_id=array();//$property_title="1 hawkesley Close";//property title will be passed from front end form where property is selected.global $wpdb;$result = mysql_query("SELECT post_id FROM sw_images_upload WHERE property_title = '$property_title' ");// Get the post IDswhile ( $row = mysql_fetch_assoc( $result ) ) {$attachments_id[]=$row['post_id'];//put all post ids for specific address into array}//extract post ids and cycle through image code to display images for selected propertyforeach ($attachments_id as $iPostID) {// Get images for all the posts for the dselected address$arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $iPostID );}//end of foreach//select all the attachment ids from the array$imagesID= wp_list_pluck($arrImages, 'ID');//set format for shortcode to use in wordpress [gallery include =N1.N2,N3 etc]$images=implode(" , ",$imagesID);//echo $images;echo do_shortcode("[gallery include={$images}]"); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.