• Решено Svetozar

    (@svetozar)


    День добрый!

    Добавил (с помощью Pods) кастомные типы Записей. Подключил к этим кастомным Записям возможность указывать встроенную Категорию(«Рубрику»).Со странички кастомной Записи могу изменять и получать список Категорий, НО со странички Категории не получаю списка кастомных Записей… WP пишет что не нашел ничего.

    При этом, получить список кастомных Записей по кастомной Таксономии — просто. И стандартные категории показывают только стандартные записи (Post).
    И, даже, в админке кастомные Записи фильтруются по Категориям!

    WP категорически (от слова Категории:)) не находит кастомные Записи! Как можно решить эту проблему? Куда смотреть? Что попробовать? (уже два часа лажу по форумам и поисковикам…)

Просмотр 7 ответов — с 1 по 7 (всего 7)
  • Try checking out this article, it should help you figure things out.

    I’m guessing you’ll need the pre_get_posts action hook to include your custom post type in the query if ( $query->is_main_query() && $query->is_tax( 'your_custom_taxonomy_here' ) ) and you just $query->set( 'post_type', $post_types ); and set $post_types up as an array of the post types you want included in that archive.

    Автор Svetozar

    (@svetozar)

    Scott, thanks! But the article is about custom Category..

    Just I want to understand how use embedding Category 🙂

    Автор Svetozar

    (@svetozar)

    Problem sovled by adding in function.php this code:

    function query_post_type($query){
    	$query->set('post_type',array('custpostype_1', 'custpostype_2'));
    }
    
    add_filter('pre_get_posts','query_post_type');

    That will replace the post_type for every query. You’ll want to at the very least do an if ( ! is_admin() && ! $query->is_main_query() ) here. You may also want to check the current post type of the query $query->get( 'post_type' ) and add yours to that list, or you may find it best to check if ( $query->is_tax( 'your_taxonomy' ) ) to be sure this only happens on the taxonomy archives as well.

    Автор Svetozar

    (@svetozar)

    Thanks! I know that I need to add some logic:

    if (is_category() || is_archive() && !is_admin()) {
    		$query->set('post_type',array('project', 'expert'));
    	}
    }

    … because if I don’t add «if» — I have trouble in admin-side. But I don’t understand: why I need to add «$query->set(‘post_type’,array(‘project’, ‘expert’));»?

    What you are doing there is changing which post types the query is for. By default, WordPress queries in the default post type, you need to change the query to get posts from ‘project’ and ‘expert’ instead.

Просмотр 7 ответов — с 1 по 7 (всего 7)

Тема «Не отображается список кастомных постов по категории» закрыта для новых ответов.