• I’m running WP 2.2 and what I’m trying to do is add two fields to the wp_posts TABLE.

    I’d like to add post_source and post_url, which I can do via phpMyAdmin. What my question is how/where do I declare these so I can use template tags in my theme to call them?

Viewing 15 replies - 1 through 15 (of 24 total)
  • What are you trying to do, exactly?

    EDIT: Maybe I should be a bit more clear in that question ?? You can add custom fields to your posts – it’s beneath the “Upload” area on the write post page. Can’t you just use that, rather than messing with your database tables?

    Thread Starter ameriblog

    (@ameriblog)

    I want to include two new fields in the TABLE, which I can add manually in phpMyAdmin.

    I have code I wrote that gets RSS feeds and inserts them into the database, the two fields I’m trying to add are from the RSS feed (RSS source and the url to the story). I can take my RSS code and have it insert into the database, as a WordPress post.

    Then I can customize the layout of my blog with themes and have it display how I want it to display.

    For example, in my index.php WP page of the theme, I’d like to just do <?php the_source(); ?>

    Hopefully that clears things up…

    I want to include two new fields in the TABLE, which I can add manually in phpMyAdmin.

    Yes, but again, you could use the “custom fields” area in the post section of the admin. Whatever you put in there, it’ll apply a name to a field in your post table, and you can do what you want with it. For example, if you wanted a new field called “oogabooga”, you go to the custom fields area and under “Custom field” you add a new “key” named “oogabooga”. Then when you write posts, you may continue to add that field on posts you choose to (it won’t do it in every posts, just ones you want it to) – then you can add a value to the “oogabooga” key. Then you can output the values for the “oogabooga” posts as you wish.

    I have code I wrote that gets RSS feeds and inserts them into the database, the two fields I’m trying to add are from the RSS feed (RSS source and the url to the story). I can take my RSS code and have it insert into the database, as a WordPress post.

    Sorry – but it sounds like you’re “scraping”?

    But the above, I guess wouldn’t work for that, because you’re wanting someone else’s content to post on your blog via RSS feed. I know you can already do that – it’s been discussed on the forums here quite a few times. Lots of links here…might as well not have to reinvent the wheel (and I’m hoping you have permission to repost the feeds you plan to post on your site, or are doing it in an ethical manner ?? )

    But if you *do* want to reinvent the wheel, you need to query your database, assign the output a variable, and then you can use that variable to place on the site within the Loop via a plugin or function.

    Thread Starter ameriblog

    (@ameriblog)

    I do have permission. I’m using SimplePie, with some customizations.

    Where, which file, do I assign the output variable? I’d like to do it exactly where WordPress is already doing so for the blog title, content, etc.

    Where, which file, do I assign the output variable? I’d like to do it exactly where WordPress is already doing so for the blog title, content, etc.

    You can call it in there, but you need to write your own function to pull the info from the database.

    So, for example, if you’ve already got the database working so it pull *in* the content (don’t know how you’re doing that, so I’m just going to go from the idea that the content is in there already), then write your query:

    function get_feed() {
    global $wpdb;
    $feedcontent = name of the database field
    $feedtitle = name of the database field
    }

    then in the index.php page (or wherever you want this to show up – but if it’s not to replace “the_content()” then you’ll have to write your own Loop somewhere else to it’ll grab all of them, not just the last one) replace “the_content();” with “get_feed();” Then it should display the content and the title.

    You’ll have to do the formatting within the functions.php file, like:

    $feedcontent = name of the database field
    $feedtitle = name of the database field
    echo '<p class="title">' . $feedtitle . '< p>';
    echo '< p>' . $feedcontent . '< /p>';

    so it’ll format properly.

    Of course, the above example is bare-bones, and probably won’t work they way it stands right now, but it should get you started.

    Thread Starter ameriblog

    (@ameriblog)

    er, double post.

    Thread Starter ameriblog

    (@ameriblog)

    It is actually working

    Thread Starter ameriblog

    (@ameriblog)

    Now I’m trying to get it to output the two fields I added.

    Right now, everything is displaying properly, what I tried to do was run a query within the WP loop that called from the wp_posts table where ID = $the_id();, but it didn’t work.

    This is my query:

    $the_post_id = the_ID();
    	$info_rs = $conn->Execute ( "SELECT post_url, ID, post_source FROM wp_posts WHERE ID =  '$the_post_id'" ) or die ( $conn->ErrorMsg() );

    Unfortunately it is displaying the ID instead of using it in the code…

    I actually tried this as well:

    function function_name() {
    		global $post;
    		$thePostID = $post->ID;
    		}

    It gives me an error that “Cannot redeclare function_name()”

    Thread Starter ameriblog

    (@ameriblog)

    Any help, please?

    It is actually working

    Wow. really? Cool. Yay me. ??

    As for the rest – yeah, you can’t redeclare a standard WP function. In fact, I really wouldn’t try to mess with the existing stuff right now. Make your own and use that instead. (You also don’t need to to the SELECT stuff – that’s what the global call to the database is for – it already does that for you.)

    So, for example, if you want the RSS feed you’ve placed into the database to be pulled in *in place* of the_content();, then you need to start by writing your own function. So in the functions.php file, you should pt the stuff I had previously (that amazingly works), and then to output it, in the index.php file, in place of <?php the_content(); ?> you put in <?php get_feed(); ?> It’ll already be inside the loop, and it *should* work, but if it doesn’t – again – that’s a place to start from.

    i also have the same problem here, i added a few fields to wp_posts table and i want to have a function that would return those values for that specific post.. i tried this:

    i included the below code in my functions.php file

    function get_fields() {
    global $wpdb;
    $fielda = dbfield;
    }

    and i included the below code in my single.php file:

    <?php get_fields(); ?>

    but it didnt work as i expected.. any advices?

    bump up..

    function get_fields() {
    global $wpdb;
    $fielda = dbfield;
    }

    That doesn’t do anything. You’re telling the function to see the database (“global $wpdb”), and you’re assigning a variable “$fielda – dbfield”), but you aren’t telling your function *what* “dbfield” is, nor how to access it, or what to take from it. Nor are you telling the function what to do with the information once you do obtain it. That alone isn’t gonna do squat.

    If you want to *get* the info, you need to call it from the database with a query. Then you need to tell the script what to do with the stuff once it obtains the information. But I can’t write that for you because 1) I don’t have time, and 2) I don’t know what you’re trying to do. But to get you started, a basic query looks like:

    $myquery = $wpdb->get_results("SELECT * FROM $wpdb->table_name WHERE something here = something else");

    Then you need to write some PHP code to tell the function what to do with the info it’s gathered.

    thank you very much for the answers.. but you see those guys up at th beginning of this thread claim that they actually get it work that way..

    BTW can i include normal php and sql codes inside my loop, will it just work fine if it is correct or will WP somehow disallow me?

    but you see those guys up at th beginning of this thread claim that they actually get it work that way

    I’m actually one of “those guys”, even if I am of the female persuasion. And there was also other stuff involved. I don’t see that you used the echo statement mentioned above in the code you gave. And you’re also forgetting the the original poster was also querying the database (they posted code for their query) – so don’t skim if you want something to work – read.

    And yes, you can use queries, et al, inside the loop, but it might be better if you at least placed the queries into the functions.php file and then called in your function. It’s just cleaner code that way.

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘Add fields to post’ is closed to new replies.