1
1
<?php
2
2
3
- class ModelBlogPost extends Model {
3
+ class ModelBlogPost extends Model
4
+ {
4
5
5
- public function getPost ( $ post_id ) {
6
+ public function getPost ($ post_id )
7
+ {
6
8
global $ wpdb ;
7
9
8
10
$ sql = "SELECT
@@ -19,12 +21,13 @@ public function getPost( $post_id ) {
19
21
20
22
$ sql .= " GROUP BY p.ID " ;
21
23
22
- $ result = $ wpdb ->get_row ( $ sql );
24
+ $ result = $ wpdb ->get_row ($ sql );
23
25
24
26
return $ result ;
25
27
}
26
28
27
- public function getPosts ( $ data = array () ) {
29
+ public function getPosts ($ data = array ())
30
+ {
28
31
global $ wpdb ;
29
32
30
33
$ sql = "SELECT
@@ -36,7 +39,7 @@ public function getPosts( $data = array() ) {
36
39
37
40
$ implode = array ();
38
41
39
- if ( isset ( $ data ['filter_category_id ' ] ) ) {
42
+ if (isset ($ data ['filter_category_id ' ]) ) {
40
43
$ implode [] = "' " . (int ) $ data ['filter_category_id ' ] . "' IN (SELECT
41
44
tt.`term_id`
42
45
FROM
@@ -47,8 +50,8 @@ public function getPosts( $data = array() ) {
47
50
AND tr.`object_id` = p.`ID`) " ;
48
51
}
49
52
50
- if ( count ( $ implode ) > 0 ) {
51
- $ sql .= ' AND ' . implode ( ' AND ' , $ implode );
53
+ if (count ($ implode ) > 0 ) {
54
+ $ sql .= ' AND ' . implode (' AND ' , $ implode );
52
55
}
53
56
54
57
$ sql .= " GROUP BY ID " ;
@@ -57,36 +60,37 @@ public function getPosts( $data = array() ) {
57
60
'ID '
58
61
);
59
62
60
- if ( isset ( $ data ['sort ' ] ) && in_array ( $ data ['sort ' ], $ sort_data ) ) {
63
+ if (isset ($ data ['sort ' ]) && in_array ($ data ['sort ' ], $ sort_data) ) {
61
64
$ sql .= " ORDER BY " . $ data ['sort ' ];
62
65
} else {
63
66
$ sql .= " ORDER BY ID " ;
64
67
}
65
68
66
- if ( isset ( $ data ['order ' ] ) && ( $ data ['order ' ] == 'DESC ' ) ) {
69
+ if (isset ($ data ['order ' ]) && ($ data ['order ' ] == 'DESC ' ) ) {
67
70
$ sql .= " DESC " ;
68
71
} else {
69
72
$ sql .= " ASC " ;
70
73
}
71
74
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 ) {
74
77
$ data ['start ' ] = 0 ;
75
78
}
76
79
77
- if ( $ data ['limit ' ] < 1 ) {
80
+ if ($ data ['limit ' ] < 1 ) {
78
81
$ data ['limit ' ] = 20 ;
79
82
}
80
83
81
84
$ sql .= " LIMIT " . (int ) $ data ['start ' ] . ", " . (int ) $ data ['limit ' ];
82
85
}
83
86
84
- $ results = $ wpdb ->get_results ( $ sql );
87
+ $ results = $ wpdb ->get_results ($ sql );
85
88
86
89
return $ results ;
87
90
}
88
91
89
- public function getTotalPosts ( $ data = array () ) {
92
+ public function getTotalPosts ($ data = array ())
93
+ {
90
94
global $ wpdb ;
91
95
92
96
$ sql = "SELECT count(*) as total
@@ -97,7 +101,7 @@ public function getTotalPosts( $data = array() ) {
97
101
98
102
$ implode = array ();
99
103
100
- if ( isset ( $ data ['filter_category_id ' ] ) ) {
104
+ if (isset ($ data ['filter_category_id ' ]) ) {
101
105
$ implode [] = "' " . (int ) $ data ['filter_category_id ' ] . "' IN (SELECT
102
106
tt.`term_id`
103
107
FROM
@@ -108,12 +112,31 @@ public function getTotalPosts( $data = array() ) {
108
112
AND tr.`object_id` = p.`ID`) " ;
109
113
}
110
114
111
- if ( count ( $ implode ) > 0 ) {
112
- $ sql .= ' AND ' . implode ( ' AND ' , $ implode );
115
+ if (count ($ implode ) > 0 ) {
116
+ $ sql .= ' AND ' . implode (' AND ' , $ implode );
113
117
}
114
118
115
- $ result = $ wpdb ->get_row ( $ sql );
119
+ $ result = $ wpdb ->get_row ($ sql );
116
120
117
121
return $ result ->total ;
118
122
}
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