最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Error function query

programmeradmin1浏览0评论

I’m trying to show a custom post type called CursosTL but I’m getting an error when using the following code.

function elc_cursos_en_el_blog() {
 
 if ( $query->is_home() && $query->is_main_query() ) {
  $query->set( 'post_type', array( 'post', 'CursoTL' ));
 }

}
add_action( 'pre_get_posts', 'elc_cursos_en_el_blog' );

The error is:

Stack trace:  
#0 wp-includes/class-wp-hook.php(287): elc_cursos_en_el_blog(Object(WP_Query))  
#1 wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters('', Array)  
#2 wp-includes/plugin.php(551): WP_Hook->do_action(Array)  
#3 wp-includes/class-wp-query.php(1779): do_action_ref_array('pre_get_posts', Array)  
#4 wp-includes/class-wp-query.php(3448): WP_Query->get_posts()  
#5 wp-includes/post.php(2065): WP_Query->query(Array)  
#6 wp-content/plugins/amp/includes/admin/functions.php(```

Any help? PHP is version 7.4.

I’m trying to show a custom post type called CursosTL but I’m getting an error when using the following code.

function elc_cursos_en_el_blog() {
 
 if ( $query->is_home() && $query->is_main_query() ) {
  $query->set( 'post_type', array( 'post', 'CursoTL' ));
 }

}
add_action( 'pre_get_posts', 'elc_cursos_en_el_blog' );

The error is:

Stack trace:  
#0 wp-includes/class-wp-hook.php(287): elc_cursos_en_el_blog(Object(WP_Query))  
#1 wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters('', Array)  
#2 wp-includes/plugin.php(551): WP_Hook->do_action(Array)  
#3 wp-includes/class-wp-query.php(1779): do_action_ref_array('pre_get_posts', Array)  
#4 wp-includes/class-wp-query.php(3448): WP_Query->get_posts()  
#5 wp-includes/post.php(2065): WP_Query->query(Array)  
#6 wp-content/plugins/amp/includes/admin/functions.php(```

Any help? PHP is version 7.4.

Share Improve this question edited Feb 21, 2021 at 11:58 Rup 4,4004 gold badges29 silver badges29 bronze badges asked Feb 20, 2021 at 16:51 Te lo cuento viajandoTe lo cuento viajando 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 3

You have to pass in the $query variable to use it in your function; if you don't, PHP uses null.

Try:

function elc_cursos_en_el_blog( $query ) {
 
 if ( $query->is_home() && $query->is_main_query() ) {
  $query->set( 'post_type', array( 'post', 'CursoTL' ));
 }

}
add_action( 'pre_get_posts', 'elc_cursos_en_el_blog' );

See pre_get_posts for the hook's documentation.

发布评论

评论列表(0)

  1. 暂无评论