Skip Navigation

[Gelöst] render_view displays wrong post type

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
It seems to be caused by the theme function that makes it possible to include custom post types in the query on the home page.

function namespace_add_custom_types( $query ) {
  if( is_front_page() || is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'nav_menu_item', 'recipe', 'news', 'feature'
        ));
      return $query;
    }
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

Solution:
For the wordpress built-in action hook pre_get_posts, please follow wordpress document to setup your custom codes:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

There is an example codes in above document:
section "Include Custom Post Types in Search Results"

Relevant Documentation:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

This support ticket is created vor 6 Jahre, 11 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 Antworten, has 2 Stimmen.

Last updated by zoeS vor 6 Jahre, 11 Monate.

Assisted by: Luo Yang.

Author
Artikel
#522302

I am trying to display a view inside a theme file using PHP, but the posts the view is displaying are the wrong type. Actually it works correctly on pages and singles, but not on archives or the front page.

I have tried:

echo do_shortcode('[wpv-view id="1135"]');
echo render_view(array('id' =>'1135'));

and I have also tried putting the view in a widget and calling the widget in the theme file, but it always has the same result – displays the correct custom post type on pages but the wrong one on archives, etc. And it is always the same post that is displayed, not one that is the same type as whatever the current archive is.

Have you any idea what might cause this?
Thank you!

#522375

Dear Zoe,

I can not duplicate same problem, please check this in your website:
1) deactivate other plugins and switch to wordpress default theme, and test again
2) Enable PHP debug mode, copy and paste the debug logs here
PHP Debugging
In case you think that Types or Views are doing something wrong (what we call a bug), you should enable PHP error logging. Again, edit your wp-config.php file and add the following:

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
This will produce a file called ‘error_log.txt’ in your WordPress root directory. Make sure that the web server can create and write this file. If it cannot, use an FTP program to create the file and make it writable to Apache (normally, user www-data).
https://toolset.com/documentation/user-guides/debugging-types-and-views/

#523041

Thank you, I have discovered that it seems to be caused by the theme function that makes it possible to include custom post types in the query on the home page. I was only using Types and not Views when I first created the site. Unless there is any way around this, I suppose I should be able to use Views to recreate the home page. This is the function:

function namespace_add_custom_types( $query ) {
  if( is_front_page() || is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'nav_menu_item', 'recipe', 'news', 'feature'
		));
	  return $query;
	}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
#523281

I assume we are talking about the wordpress built-in action hook pre_get_posts, I suggest you follow wordpress document to setup your custom codes:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

There is an example codes in above document:
section "Include Custom Post Types in Search Results"

For your reference.

#523423

Thank you so much I got there eventually! The conflict was coming from the fact that I was filtering the post types in the functions file and then filtering the categories in the home page template.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.