11<?php
22
3- class ModelBlogPost extends Model {
3+ class ModelBlogPost extends Model
4+ {
45
5- public function getPost ( $ post_id ) {
6+ public function getPost ($ post_id )
7+ {
68 global $ wpdb ;
79
810 $ sql = "SELECT
@@ -19,12 +21,13 @@ public function getPost( $post_id ) {
1921
2022 $ sql .= " GROUP BY p.ID " ;
2123
22- $ result = $ wpdb ->get_row ( $ sql );
24+ $ result = $ wpdb ->get_row ($ sql );
2325
2426 return $ result ;
2527 }
2628
27- public function getPosts ( $ data = array () ) {
29+ public function getPosts ($ data = array ())
30+ {
2831 global $ wpdb ;
2932
3033 $ sql = "SELECT
@@ -36,7 +39,7 @@ public function getPosts( $data = array() ) {
3639
3740 $ implode = array ();
3841
39- if ( isset ( $ data ['filter_category_id ' ] ) ) {
42+ if (isset ($ data ['filter_category_id ' ]) ) {
4043 $ implode [] = "' " . (int ) $ data ['filter_category_id ' ] . "' IN (SELECT
4144 tt.`term_id`
4245 FROM
@@ -47,8 +50,8 @@ public function getPosts( $data = array() ) {
4750 AND tr.`object_id` = p.`ID`) " ;
4851 }
4952
50- if ( count ( $ implode ) > 0 ) {
51- $ sql .= ' AND ' . implode ( ' AND ' , $ implode );
53+ if (count ($ implode ) > 0 ) {
54+ $ sql .= ' AND ' . implode (' AND ' , $ implode );
5255 }
5356
5457 $ sql .= " GROUP BY ID " ;
@@ -57,36 +60,37 @@ public function getPosts( $data = array() ) {
5760 'ID '
5861 );
5962
60- if ( isset ( $ data ['sort ' ] ) && in_array ( $ data ['sort ' ], $ sort_data ) ) {
63+ if (isset ($ data ['sort ' ]) && in_array ($ data ['sort ' ], $ sort_data) ) {
6164 $ sql .= " ORDER BY " . $ data ['sort ' ];
6265 } else {
6366 $ sql .= " ORDER BY ID " ;
6467 }
6568
66- if ( isset ( $ data ['order ' ] ) && ( $ data ['order ' ] == 'DESC ' ) ) {
69+ if (isset ($ data ['order ' ]) && ($ data ['order ' ] == 'DESC ' ) ) {
6770 $ sql .= " DESC " ;
6871 } else {
6972 $ sql .= " ASC " ;
7073 }
7174
72- if ( isset ( $ data ['start ' ] ) || isset ( $ data ['limit ' ] ) ) {
73- if ( $ data ['start ' ] < 0 ) {
75+ if (isset ($ data ['start ' ]) || isset ($ data ['limit ' ]) ) {
76+ if ($ data ['start ' ] < 0 ) {
7477 $ data ['start ' ] = 0 ;
7578 }
7679
77- if ( $ data ['limit ' ] < 1 ) {
80+ if ($ data ['limit ' ] < 1 ) {
7881 $ data ['limit ' ] = 20 ;
7982 }
8083
8184 $ sql .= " LIMIT " . (int ) $ data ['start ' ] . ", " . (int ) $ data ['limit ' ];
8285 }
8386
84- $ results = $ wpdb ->get_results ( $ sql );
87+ $ results = $ wpdb ->get_results ($ sql );
8588
8689 return $ results ;
8790 }
8891
89- public function getTotalPosts ( $ data = array () ) {
92+ public function getTotalPosts ($ data = array ())
93+ {
9094 global $ wpdb ;
9195
9296 $ sql = "SELECT count(*) as total
@@ -97,7 +101,7 @@ public function getTotalPosts( $data = array() ) {
97101
98102 $ implode = array ();
99103
100- if ( isset ( $ data ['filter_category_id ' ] ) ) {
104+ if (isset ($ data ['filter_category_id ' ]) ) {
101105 $ implode [] = "' " . (int ) $ data ['filter_category_id ' ] . "' IN (SELECT
102106 tt.`term_id`
103107 FROM
@@ -108,12 +112,31 @@ public function getTotalPosts( $data = array() ) {
108112 AND tr.`object_id` = p.`ID`) " ;
109113 }
110114
111- if ( count ( $ implode ) > 0 ) {
112- $ sql .= ' AND ' . implode ( ' AND ' , $ implode );
115+ if (count ($ implode ) > 0 ) {
116+ $ sql .= ' AND ' . implode (' AND ' , $ implode );
113117 }
114118
115- $ result = $ wpdb ->get_row ( $ sql );
119+ $ result = $ wpdb ->get_row ($ sql );
116120
117121 return $ result ->total ;
118122 }
119- }
123+
124+ public function getCategoriesByPost ($ post_id )
125+ {
126+ global $ wpdb ;
127+
128+ $ sql = "SELECT
129+ tt.`term_id`
130+ FROM
131+ `wp_term_relationships` tr
132+ LEFT JOIN `wp_term_taxonomy` tt
133+ ON tr.`term_taxonomy_id` = tt.`term_taxonomy_id`
134+ WHERE tt.`taxonomy` = 'category'
135+ AND tr.`object_id` = ' " . $ post_id . "' " ;
136+
137+
138+ $ result = $ wpdb ->get_results ($ sql );
139+
140+ return $ result ;
141+ }
142+ }
0 commit comments