Home Forums Backend Issues (wp-admin) Parsing results of db query


Parsing results of db query

  • I have a CPT that uses ACF to keep track of (among other things) request statuses. Right now, I’m trying to make a filter in the admin panel. I want to get all the possible statuses and display them to the user in a select box. But, I’ve run into an issue. How do I parse the results of my database query in a way that make sense?

    I’m using the following to get access to the post where the statuses are kept.

    $query = "
    		SELECT DISTINCT post_content
    		FROM $wpdb->posts
    		WHERE post_title LIKE 'request_status'
    	// gets an array of statuses... but only ones that have been used so far. 
    	$statuses = $wpdb->get_row($query, ARRAY_A);

    Even though this query works, it returns this. Which is just one long string.
    a:13:{s:4:"type";s:6:"select";s:12:"instructions";s:0:"";s:8:"required";i:0;s:17:"conditional_logic";i:0;s:7:"wrapper";a:3:{s:5:"width";s:5:"33.33";s:5:"class";s:0:"";s:2:"id";s:0:"";}s:7:"choices";a:11:{s:0:"";s:0:"";s:8:"Received";s:8:"Received";s:17:"Premature request";s:17:"Premature request";s:23:"Clarification requested";s:23:"Clarification requested";s:11:"In progress";s:11:"In progress";s:8:"Complete";s:8:"Complete";s:17:"Request withdrawn";s:17:"Request withdrawn";s:20:"No records available";s:20:"No records available";s:28:"Denied - Insufficient Detail";s:28:"Denied - Insufficient Detail";s:23:"Denied - Confidential 1";s:23:"Denied - Confidential 1";s:23:"Denied - Confidential 2";s:23:"Denied - Confidential 2";}s:13:"default_value";a:0:{}s:10:"allow_null";i:0;s:8:"multiple";i:0;s:2:"ui";i:0;s:4:"ajax";i:0;s:11:"placeholder";s:0:"";s:13:"return_format";s:5:"value";}

    Am I not constructing the query correctly? Is there a different/better way of making this request? Thanks for any help you can offer!

  • Well – I sort of solved this, but I’m still curious as to how to parse the above string. To get around this I used…

    global $wpdb;
    	$query = "
    		SELECT DISTINCT post_name
    		FROM $wpdb->posts
    		WHERE post_title LIKE 'request_status'
    	$result = $wpdb->get_row($query, ARRAY_N);
    	$field_object = get_field_object($result[0]);
    	$status_array = $field_object['choices'];

    this gives an array of choices from the field object instead of trying to read them from the database

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Parsing results of db query’ is closed to new replies.