Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter FueledPub

    (@fueledpub)

    Let me retract that last post… In fact, that does absolutely nothing to resolve the issue.

    The problem is the wp function get_post_custom returns data randomly, not by meta_id as you might expect… Once I realized this, I wrote a quick function to replace it and add more flexibility…

    /*---------------------------------------------------------------
      Function: Retrieve post meta field for a post based on ID
      Usage: [admin/meta_box/downoads.php] = to replace core wp get_post_custom function
      Params: $id = post id, $key = meta key, $order = ASC|DESC,
      	$return = meta_value|meta_key|post_id|meta_id, $single = true|false
      @return = result will be array unless $single = true, which will return the last result only
    ----------------------------------------------------------------*/
    function noconflict_get_post_custom($id,$key,$single=false,$column='meta_value',$order='ASC'){
    	global $wpdb;
    	$data = $wpdb->get_results("
    	SELECT * FROM $wpdb->postmeta WHERE post_id = $id
    	AND meta_key LIKE '$key'
    	ORDER by meta_id $order
    	");
    
    	if( !empty( $data ) ) {
    		$metadata = array();
    		foreach( $data as $result_object ) {
    			$metadata[] = $result_object->$column;
    		}
    		if ($single)
    			return $metadata[0];
    		else
    			return $metadata;
    	}
    }

    This works perfectly…

    FueledPub

    (@fueledpub)

    Check this out…

    https://wplift.com/how-to-create-a-custom-login-page-for-your-wordpress-theme

    Just create a simple plugin that creates a shortcode and calls a function with your modified code or something similar to above…

    FueledPub

    (@fueledpub)

    I knwo the Register Redux Plus plugin add this functionality… perhaps check it out?

    Thread Starter FueledPub

    (@fueledpub)

    So the solution is to simply define names for the keys. For example instead of:

    <input style=\"width:25%;\" name=\"wsm-content-downloads[]\" onblur=\"if (this.value == '') { this.value='Download/Resource Name...';} \" onfocus=\" if (this.value == 'Download/Resource Name...') { this.value = ''; }\" value=\"Download/Resource Name...\" />

    Using this:

    <input style=\"width:25%;\" name=\"wsm-content-downloads['name']\" onblur=\"if (this.value == '') { this.value='Download/Resource Name...';} \" onfocus=\" if (this.value == 'Download/Resource Name...') { this.value = ''; }\" value=\"Download/Resource Name...\" />

    The thing that still stumps me is I don’t understand why this even matters.

    I’m assuming the wp function add_post_meta does not save the data as a serialized array but instead creates new rows for every record in the array I pass to the save function…

    Then, when wp function get_post_custom grabs the custom field data is returns an array of the values of rows where meta key is _______…

    And apparently it doesn’t return them in the same order they were added to the database, which seems weird to me. And it’s even weirder that by simply adding a name for the key that it resolves the issue.

    …and yet even weirder that the original code works perfectly on my local (MAMP) – same WP version – same all around and doesn’t work on my live site…

    So many questions ??

    Bryan

    Thread Starter FueledPub

    (@fueledpub)

    Esmi,

    Thanks for the suggestion, but that’s not really what I’m looking to do… I need the data saved as an array in single meta key. It’s not really efficient for me to save each piece of data as a separate custom field.

    Thanks though…

    Bryan

Viewing 5 replies - 1 through 5 (of 5 total)