0

I can't get my custom posts with a specific taxonomy. Here is my code in functions.php :

function portalp_custom_post_type_realisations() {
    $labels = array(
        'name'                  => _x("Réalisations", 'Post type general name', 'textdomain'),
        'singular_name'         => _x("Réalisation", 'Post type singular name', 'textdomain'),
        'menu_name'             => _x("Réalisations", 'Admin Menu text', 'textdomain'),
        'add_new'               => __("Ajouter", 'textdomain'),
        'add_new_item'          => __("Ajouter une réalisation", 'textdomain'),
        'new_item'              => __("Nouvelle réalisation", 'textdomain'),
        'edit_item'             => __("Modifier une réalisation ", 'textdomain'),
        'view_item'             => __("Voir la réalisation", 'textdomain'),
        'all_items'             => __("Toutes les réalisations", 'textdomain'),
        'search_items'          => __("Rechercher des réalisations", 'textdomain'),
        'not_found'             => __("Aucune réalisation trouvée.", 'textdomain'),
        'not_found_in_trash'    => __("Aucune réalisation trouvée dans la corbeille.", 'textdomain'),
        'featured_image'        => _x("Image à la une de la réalisation", 'Overrides the “Featured Image” phrase for this post type.                                        Added in 4.3', 'textdomain'),
        'set_featured_image'    => _x('Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3',                                       'textdomain'),
        'remove_featured_image' => _x('Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in                                       4.3', 'textdomain'),
        'use_featured_image'    => _x('Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in                                       4.3', 'textdomain'),
        'archives'              => _x("Toutes les réalisations", 'The post type archive label used in nav menus. Default “Post                                            Archives”. Added in 4.4', 'textdomain'),
        'insert_into_item'      => _x("Insérer dans la réalisation", 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain'),
        'uploaded_to_this_item' => _x('Uploaded to this book', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain'),
        'filter_items_list'     => _x('Filter books list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain'),
        'items_list_navigation' => _x('Books list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain'),
        'items_list'            => _x('Books list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain'),
    );

    $args = array(
        'description'           => __("Les réalisations de Portalp"),
        'labels'                => $labels,
        'supports'              => array('title', 'revisions', 'custom-fields', 'thumbnail', 'post-formats'),
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'show_in_rest'          => true,
        'menu_position'         => 4,
        'menu_icon'             => 'dashicons-format-gallery',
        'map_meta_cap'          => true,
    );

    // Enregistrement du Custom Post Type
    register_post_type('realisations', $args);
}

add_action('init', 'portalp_custom_post_type_realisations');

/**
 * Déclaration des taxonomies personnalisées pour Portalp
 */
function portalp_custom_taxonomies() {
    
    // Déclaration de la taxonomie "Transports"

    $labels_transports = array(
        'name'            => _x( 'Transports', 'taxonomy general name'),
        'singular_name'   => _x( 'Transport', 'taxonomy singular name'),
        'search_items'    => __( 'Chercher un transport'),
        'all_items'       => __( 'Tous les transports'),
        'edit_item'       => __( 'Editer le transport'),
        'update_item'     => __( 'Mettre à jour le transport'),
        'add_new_item'    => __( 'Ajouter un nouveau transport'),
        'new_item_name'   => __( 'Valeur du nouveau transport'),
        'menu_name'       => __( 'Transports'),
    );

    $args_transports = array(
        'description'       => 'Type de transport de la réalisation',
        'public'            => false,
        'hierarchical'      => true,
        'labels'            => $labels_transports,
        'show_ui'           => true,
        'show_in_rest'      => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'transports' ),
    );

    register_taxonomy('transports', array('realisations'), $args_transports);
    
    
    // Déclaration de la taxonomie "Solutions"

    $labels_solutions = array(
        'name'            => _x( 'Solutions', 'taxonomy general name'),
        'singular_name'   => _x( 'Solution', 'taxonomy singular name'),
        'search_items'    => __( 'Chercher une solution'),
        'all_items'       => __( 'Toutes les solutions'),
        'edit_item'       => __( 'Editer la solution'),
        'update_item'     => __( 'Mettre à jour la solution'),
        'add_new_item'    => __( 'Ajouter une nouvelle solution'),
        'new_item_name'   => __( 'Valeur de la nouvelle solution'),
        'menu_name'       => __( 'Solutions'),
    );

    $args_solutions = array(
        'description'       => 'Catégories de produits installés sur les chantiers',
        'public'            => false,
        'hierarchical'      => true,
        'labels'            => $labels_solutions,
        'show_ui'           => true,
        'show_in_rest'      => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'solutions' ),
    );

    register_taxonomy('solutions', 'realisations', $args_solutions);
    add_action('init', 'portalp_custom_taxonomies');
    $args = array(
        'post_type' => "realisations",
        'tax_query' => array(
            array(
                'taxonomy'  => 'transports',
                'field' => 'slug',
                'terms' => "mini-metro",
            ),
        ),
    );
    $query = new WP_Query($args);

    echo '<pre>' . var_dump($query) . '</pre>';

    die();

The var_dump works fine when i don't specify the tax_query parameters. But when i set the tax_query that doesn't work anymore. Do you have any idea why this code doesn't work please?

Thank you on advance.

4
  • Sorry i was impolite, I didn't say hello to you in the introduction, and i can't edit my post... :)
    – Jérémie
    Mar 17 at 14:49
  • 1
    Welcome! Generally introductions are better left out since this isn't a message board site, so best to keep it that way :)
    – YourManDan
    Mar 17 at 16:06
  • 2
    I don't see that you've closed off your portal_p_custom_taxonomies() function with a closing } before calling add_action(). Is this the full code? That would cause your problem since the add_action() function isn't getting called because it's within the function it's calling.
    – YourManDan
    Mar 17 at 16:08
  • @Jérémie your custom transports taxonomy is not public, i.e. 'public' => false. Was that intentional?
    – Sally CJ
    Mar 17 at 16:13

2 Answers 2

0

It appears that you're attempting to add a function to the init action hook from within the function you're trying to add.

Your code is doing this:

function portalp_custom_taxonomies() {
    // ...
    add_action( 'init', 'portalp_custom_taxonomies' );
    // ...
}

...when it needs to do this:

function portalp_custom_taxonomies() {
    // ...
}
add_action( 'init', 'portalp_custom_taxonomies' );

As presented, the portalp_custom_taxonomies() function will not run (unless you're calling it somewhere else) and so the taxonomies inside it aren't getting registered.

0

WP_Query is not used in functions.php. While it could be used in certain circumstances, it is too easy to break main loop. In Theme developer manual you have it described how to do it, you typically use query in a template. It is not clear if you want that loop as a primary or secondary loop, if you want it as secondary, then you also have to call wp_reset_postdata() after the loop, again to not mess the primary loop variables. If it is used as secondary loop, I find it easier to use get_posts() function, which retrieves array of posts and does not mess with main loop variables. Again, you would typically call this in a template, not in functions.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.