How to use the_post action hook
-
Hi! I use filter the_posts, that create posts list from my specific table:
function add_special_posts($posts) { global $wpdb; if (is_category('11')) { $my_table_name = $wpdb->prefix . "posts"; $sql_select = "SELECT * FROM " . $my_table_name; $result = $wpdb->get_results($sql_select, ARRAY_A); $posts = array(); foreach ($result as $mytable_value) { $post = new stdClass(); $post->ID = $mytable_value['id']; $post->post_author = '1'; $post->post_date = date('Y-m-d H:m:s'); $post->post_type = 'post'; $post->post_title = $mytable_value['portfolio_name']; $post->post_content = $mytable_value['full_description']; $post->post_status = 'open'; $post->comment_status = 'open'; $post->ping_status = 'open'; $posts[] = $post; } } return $posts; }
It works fine: i get posts list from my specific table in category with id=11. Now I want to view a single post page with data from my_table_name. I suspect that I use the_post action hook or may be loop_start action hook.
When I use something like as:function view_single_special_post($post) { global $wpdb; if (is_single() && in_category('11',$post->ID)) { $portfolio_table_name = $wpdb->prefix . "portfolio"; $sql_select = "SELECT * FROM " . $portfolio_table_name; $result = $wpdb->get_results($sql_select, ARRAY_A); foreach ($result as $mytable_value) { $post->ID = $mytable_value['id']; $post->post_author = '1'; $post->post_date = date('Y-m-d H:m:s'); $post->post_type = 'post'; $post->post_title = $mytable_value['portfolio_name']; $post->post_content = $mytable_value['full_description']; $post->post_status = 'open'; $post->comment_status = 'open'; $post->ping_status = 'open'; } } return $post; } add_action('the_post', 'view_single_special_post');
It doesn’t work for category wiht id=11, which has data from my specific table $my_table_name, but works for other categories. I write it at the plugin file.
Help me please and sorry for my English.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘How to use the_post action hook’ is closed to new replies.