Can someone tell me the comparison of these three query statement:
1/ Is it nline code ? I don't know what is it call.
$getposts->query('post_type=projects&taxonomy=projects_categories&post_status=publish&showposts=-1&field=term_id&terms='.$cat_id);
2/
$args2 = array(
'post_type' => 'projects',
'taxonomy' => 'projects_categories',
'posts_per_page' => 9,
'orderby' => 'date',
'order' => 'DESC',
'terms' => $cat_id,
);
$getposts->query($args2);
3/
$args3 = array(
'posts_per_page' => 9,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => array (
'projects' => 'projects',
),
'tax_query' => array(
array(
'taxonomy' => 'projects_categories',
'terms' => $cat_id,
)
)
);
$getposts->query($args3);
Why my 1st and 2nd code doesn't work in my case. I have to use the 3rd one to display all the post (custom post type) that belong to the taxonomy that have $cat_id
I google and someone have the same problem that they can't display post in taxonomy. Finally they use some similar to the 3rd code. Can someone show me how to switch back and forth and when we should use the 1st, 2nd or 3rd query.
Why sometime we use 'post_per_page' and sometime we use 'showposts'?
Thank you
Can someone tell me the comparison of these three query statement:
1/ Is it nline code ? I don't know what is it call.
$getposts->query('post_type=projects&taxonomy=projects_categories&post_status=publish&showposts=-1&field=term_id&terms='.$cat_id);
2/
$args2 = array(
'post_type' => 'projects',
'taxonomy' => 'projects_categories',
'posts_per_page' => 9,
'orderby' => 'date',
'order' => 'DESC',
'terms' => $cat_id,
);
$getposts->query($args2);
3/
$args3 = array(
'posts_per_page' => 9,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => array (
'projects' => 'projects',
),
'tax_query' => array(
array(
'taxonomy' => 'projects_categories',
'terms' => $cat_id,
)
)
);
$getposts->query($args3);
Why my 1st and 2nd code doesn't work in my case. I have to use the 3rd one to display all the post (custom post type) that belong to the taxonomy that have $cat_id
I google and someone have the same problem that they can't display post in taxonomy. Finally they use some similar to the 3rd code. Can someone show me how to switch back and forth and when we should use the 1st, 2nd or 3rd query.
Why sometime we use 'post_per_page' and sometime we use 'showposts'?
Thank you
Share Improve this question asked Jun 6, 2019 at 0:45 Gibson TranGibson Tran 1 1- The first example is using query string syntax. It might work, but I'm not sure if it properly supports taxonomy queries, but regardless, there's no good reason to be using that syntax these days. It just makes it harder to read and debug. – Jacob Peattie Commented Jun 7, 2019 at 8:43
1 Answer
Reset to default 2Your 3rd option is correct.
$args3 = array(
'posts_per_page' => 9,
'orderby' => 'date',
'order' => 'DESC',
// If you're only specifying one. Use array( 'projects', 'post' ); for many.
'post_type' => 'projects',
// This is the correct syntax for a taxonomy query.
'tax_query' => array(
array(
'taxonomy' => 'projects_categories',
'terms' => $cat_id,
),
),
);
$getposts->query( $args3 );
The other options you have posted here are old methods that are no longer valid for custom taxonomy queries. Always reference to WP_Query Codex to make sure you're using the latest parameters.
As for your second question, posts_per_page
replaced showposts
. showposts
is no longer valid.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745433933a4627520.html
评论列表(0条)