Search code examples
phpwordpresswoocommercecustom-post-typeendpoint

Woocommerce My Account: get custom post type post ID from post author


On the site, users fill in a form that creates their page on the website to display information. Within the My Account page, I've created a link that takes the logged-in user to the WordPress dashboard where they can edit this page.

// Add Store link WooCommerce Navigation
add_filter ( 'woocommerce_account_menu_items', 'one_more_link' );
function one_more_link( $menu_links ){
    $new = array( 'stall-page' => 'Edit Stall Information' );

    $menu_links = array_slice( $menu_links, 0, 1, true ) 
    + $new 
    + array_slice( $menu_links, 1, NULL, true );

    return $menu_links;
}

// hook the URL
add_filter( 'woocommerce_get_endpoint_url', 'hook_endpoint', 10, 4 );
function hook_endpoint( $url, $endpoint, $value, $permalink ){
    if( 'stall-page' === $endpoint ) {
        $url = 'https://websitename.com/wp-admin/edit.php?post_type=stallholders';
    }
    return $url;
}

I want to take it one step further and link directly to the page they need to edit.

I've been experimenting and gotten as far as this:

// hook the URL
add_filter( 'woocommerce_get_endpoint_url', 'hook_endpoint', 10, 4 );
function hook_endpoint( $url, $endpoint, $value, $permalink ){
    $current_user = wp_get_current_user();
    $post_id = $current_user->post_ID;
    if( 'stall-page' === $endpoint ) {
        $url = 'https://websitename.com/wp-admin/post.php?post=' . $post_id . '&action=edit';
    }
    return $url;
}

But this is linking to the users ID and not their custom post type page?


Solution

  • You just need to query the "stallholders" custom posts from the user and use the queried post ID in the URL.

    Try the following replacement code:

    // Add Store link WooCommerce Navigation
    add_filter ( 'woocommerce_account_menu_items', 'one_more_link' );
    function one_more_link( $menu_links ){
        $new = array( 'stall-page' => __('Edit Stall Information', 'woocommerce') );
    
        $menu_links = array_slice( $menu_links, 0, 1, true ) 
        + $new 
        + array_slice( $menu_links, 1, NULL, true );
    
        return $menu_links;
    }
    
    // Define 'stall-page' endpoint URL
    add_filter( 'woocommerce_get_endpoint_url', 'define_stall_page_endpoint_url', 10, 4 );
    function define_stall_page_endpoint_url( $endpoint_url, $endpoint, $value, $permalink ){
        global $current_user;
    
        // Get stallholders custom posts IDs from the user
        $post_ids = get_posts( array(
            'numberposts'   => 1,
            'post_author'   => $current_user->ID,
            'post_type'     => 'stallholders',
            'fields'        => 'ids',
        ));
    
        if( 'stall-page' === $endpoint && ! empty($post_ids) ) {
            return admin_url('post.php?post='.reset($post_ids).'&action=edit');
        }
        return $endpoint_url;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work.