Support » Plugins » Hacks » wp_posts custom mysql column name

  • Hi!
    I don’t to use wp_postsmeta but wp_posts. I have inserted a custom column name in MySQL table wp_posts , named “test_col”.
    I want to be able to use wp_insert_post to populate that custom column in mysql, like this :

    // Create post object
    $my_post = array(
      'post_title'    => 'My post',
      'post_content'  => 'This is my post.',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'test_col' => 'whatever'

    // Insert the post into the database

    wp_insert_post( $my_post );

    and also .. how can i gat that value out when get_posts()
    Thank you !

Viewing 4 replies - 1 through 4 (of 4 total)
  • and here is the solution ..
    adding the following in the theme’s functions.php

    add_filter( ‘wp_insert_post_data’, ‘wp_insert_post_data_filter’,1, 2 );

    function wp_insert_post_data_filter( $data, $postarr ) {
    @    $data['test_col'] = $postarr['custom_data'];
    return $data;

    .. where ‘test_col’ is the column name from wp_posts
    i have put the @ in front of lines because I use it to post via XMLRPC and i was getting some errors while i was not submitting all the values for all variables.

    i hope this thing is useful for others

    Hi, I am having very similar problem I would love to solve.

    I have in my wp_posts table a column called post_region

    so when querying from a search form ($search_query contains the search term as in [s] => search term) my array will be.

    $array2 = array(
    			'post_region' => '4'
    $search_query = array_merge((array)$search_query, (array)$array2);
    query_posts($search_query );

    The search query comes through fine but post_region is ignored, I assume the code you have provided maybe registers the new column but I can’t get it to work.
    SO say for example I include ‘post_type’ => ‘post’

    That gets recognised, so my code is fine apart from obviously the that query_posts() function isn’t setup to recognise it.

    Any idea’s thanks

    I found the solution here

    using the query filter posts_where

    Strangely when I apply a filter it will display all the post types/ ie pages/revisons and posts in the results, whereas before it just displayed the posts.

    It’s easy to set the filter to pagetype=whatever but something to look out for, I’m not sure I assume it does this as the posts_where writes over a certain part of the query

    I’ve just realised that it overwrites the entire search query aswell

    so my search items become balnk but my filters are recognised

    global $query_string;
    $query_args = explode("&", $query_string);
    $search_query = array();
    foreach($query_args as $key => $string) {
    	$query_split = explode("=", $string);
    	$search_query[$query_split[0]] = urldecode($query_split[1]);
    } // foreach
    //applying filters somewhere here the search query becomes lost
    $search = new WP_Query();
    add_filter('posts_where', 'filter_where');
    // loop
    if ($search->have_posts()) {
    	while ($search->have_posts()) {$search->the_post(); ?>
    <?php include('/include/post_list.php') ?>
    <?php } }

    and in my funtions.php

    function filter_where(){
       $where .= "AND (wp_posts.post_region = '4')";
    	return $where;

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘wp_posts custom mysql column name’ is closed to new replies.