• I’m sure I could solve it by massive hacking, but I’d prefer to know the elegant solution offered by WordPress gurus…

    I have “custom fields”, named “Interview”. The value of this field is usually a link. Since my site is going to be a quote-collection site, I add many posts with the exact same metadata. Along that it’s a bit inconvenient to always copy/paste the “Interview” field; it would be very unpleasant, if some publishers relocated their interviews, then I’d need to change the “Interview” fields one by one.

    I have an idea – I should have a distinct database table to hold interviews, then I’d only need to refer to them by their IDs. For example, when I publish my post, I supply “_14” for the “Interview” custom field, and WordPress would fetch the corresponding row from the interviews table, and show that. If an interview’s location changes, I only need to modify the corresponding row in the interviews table.

    Any ideas how could I solve it elegantly? I mean, where should I add these modifications? And of course, for performance optimization, pages those show more than one posts should fetch the interviews table only once!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Don’t need any table. You edit/ add any post there you have get a custom field box below the editor. Add the “Interview” custom field and put the value. Thereafter you put the following code in your corresponding php file.

    <?php
     $interviewer = get_post_meta($post->ID, "Interview", ture);
      if($interviewer !="") echo $interviewer;
    ?>

    Thread Starter megabrutal

    (@megabrutal)

    I don’t really get it… I’ve added a the_meta() call in my templates so my metadatas are shown well. My problem is that I wouldn’t like to add the exact same and long metadata values to a bunch of posts. It’s inconvenient, redundant, and it would be a problem if I’d need to change these metas for all posts for some reason.

    Thread Starter megabrutal

    (@megabrutal)

    Meanwhile, I figured it out myself.

    I wrote a small plugin that does the work:

    <?php
    /*
    Plugin Name: Replace Custom Field By Database Entry
    Version: 0.1
    Author: MegaBrutal
    */
    
    function rcfbde_install () {
    	global $wpdb;
    
    	$table_name = $wpdb->prefix . "cftolink";
    	$sql = "CREATE TABLE IF NOT EXISTS '" . $table_name . "' (
    			'id' mediumint unsigned NOT NULL AUTO_INCREMENT,
    			'title' text NOT NULL,
    			'url' text DEFAULT NULL,
    			PRIMARY KEY ('id')
    		);";
    
    	$sql = $wpdb->prepare($sql);
    	$wpdb->query($sql);
    }
    
    function filter_rcfbde ($line, $key, $value) {
    	global $wpdb;
    
    	$table_name = $wpdb->prefix . "cftolink";
    
    	$valuet = trim($value);
    	if ('_' == $valuet[0]) {
    		$valuen = substr($valuet, 1);
    		$row = wp_cache_get($valuen, "cftolink");
    		if (false == $row) {
    			$sql = $wpdb->prepare("SELECT * FROM '" . $table_name . "' WHERE 'id'=" . $valuen . ";");
    			$row = $wpdb->get_row($sql);
    			wp_cache_set($valuen, $row, "cftolink");
    		}
    
    		if (is_null($row->url)) {
    			$line = str_replace($value, $row->title, $line);
    		} else {
    			$ahref = "<a href=\"" . $row->url . "\">" . $row->title . "</a>";
    			$line = str_replace($value, $ahref, $line);
    		}
    	}
    	return $line;
    }
    
    register_activation_hook(__FILE__, 'rcfbde_install');
    add_filter('the_meta_key', 'filter_rcfbde', 1, 3);
    ?>

    I hope I chose the most efficient solution.

    son0fhobs

    (@son0fhobs)

    My question is similar, but relates to the “most efficient solution.”

    I’m storing data, and I can either store it directly in the database, or in custom fields. It will probably be 5-9 (cells?) of data, so 5-9 custom fields per post, or that many rows per post in a mysql table.

    What would be the pros/cons? Is there much of a speed difference? Obviously custom fields work with the database, so I’m guessing going straight to the database would be more efficient, yet I’m far more comfortable with custom fields. If I’m building a large site, speed could really add up making a difference. Yet part of me wonders if it’s pure syntax difference, and speed would be the same.

    Any thoughts?

    Thanks all!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Field from database table’ is closed to new replies.