Getting data from custom table

Support MB Custom Table Getting data from custom table

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #48949
    hlikeshlikes
    Participant

    In reference to: https://docs.metabox.io/extensions/mb-custom-table/#get

    $data = \MetaBox\CustomTable\API::get( $object_id, $table, $force );

    I have a custom model set up and saving data successfully to its custom table.

    I want to display data from that table, but if I don't have a specific $object_id to query, is there another method available to use if I want to fetch a single row based on certain criteria?

    For example, the first result when the dates in a datetime column is sorted via ASC?

    #48954
    PeterPeter
    Moderator

    Hello,

    Thanks for reaching out.

    You can use the SQL query to query data from the custom table based on some values. Refer to the documentation
    https://docs.metabox.io/extensions/mb-custom-table/#query-posts-with-wp_query

    #48955
    hlikeshlikes
    Participant

    Thanks,

    I was hoping there would be more useful methods available to retrieve data for custom models with custom tables, without having to resort to SQL.

    #48962
    PeterPeter
    Moderator

    Hello,

    I'm afraid that there aren't other methods to get data from a custom table without ID. We also use the SQL query to get the data, please check this function

    private static function query_get( int $object_id, string $table ): array {
    	global $wpdb;
    
    	$row = $wpdb->get_row(
    		$wpdb->prepare(
    			"SELECT * FROM $table WHERE <code>ID</code> = %d",
    			$object_id
    		),
    		ARRAY_A
    	);
    
    	return is_array( $row ) ? $row : [];
    }

    file /wp-content/plugins/meta-box-aio/vendor/meta-box/mb-custom-table/src/Cache.php

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.