Using while and foreach with $wpdb
-
Hi
I’m using ACF (Advanced Custom Fields) on my backend. I have a post_type named “rundowns” to make some lists of posts from another post_type named “elements”. On even rundown post i am using ACF to make an array with the ID’s of the posts from the post_type “elements”.
Now I want to display all the posts on each rundown-post-page (single-rundown.php) – but I want to change the order of the list with drag-and-drop.
I got these codes, but I can’t get the results from the MySQL.
In the top of single-rundown.php:
get_header(); function get_records() { //$con=connectsql(); $records=$wpdb->query("SELECT * FROM $wpdb->posts WHERE post_type = 'elements' AND post_status = 'publish'"); $all=array(); while($data=$records->fetch_assoc()) { $all[]=$data; } return $all; }
And here the code for displaying:
<ul id="sortable"> <?php $data=get_records(); ?> <?php foreach($data as $record): ?> <li data-id="<?php echo $record['id']; ?>" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span><?php echo $record['id']; ?><?php echo $record['name']; ?></li> <?php endforeach; ?> </ul> <button id="save-reorder">Save</button>
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Using while and foreach with $wpdb’ is closed to new replies.