custom post types - Wordpress wp_query() basic question about args

Can someone tell me the comparison of these three query statement:1 Is it nline code ? I don't know what is it cal

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
Add a comment  | 

1 Answer 1

Reset to default 2

Your 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

相关推荐

  • custom post types - Wordpress wp_query() basic question about args

    Can someone tell me the comparison of these three query statement:1 Is it nline code ? I don't know what is it cal

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信