שאילתות SQL בוורדפרס
WP_Query הוא אובייקט שאפשר להעשות באמצעותו שאילתות על פוסטים בוורדפרס. דוגמאות
$query = new WP_Query( array( 'tag' => 'cooking' ) );
מחזיר את הפוסטים שיש להם טאג cooking.
$query = new WP_Query( array( 'tag__not_in' => array( 37, 47 ) ) );
מחזיר את הפוסטים שאין שה tag_id שלהם אינו id 37 ו 47.
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob',
),
),
);
$query = new WP_Query( $args );
המון מידע ודוגמאות נוספות יש בדוקומנטציה
https://codex.wordpress.org/Class_Reference/WP_Query
לאחר השילתא כדי להוציא את הנתונים על הפוסט צריך לעבור עם לולאה של have_post
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}