0

So i have 2 post types 'loads' and 'invoices'. Invoices is also used to show loads posts. I need not Invoices posts at all.

What I have Done: I used this code to show loads posts on Invoice page.

add_action( 'pre_get_posts', 'join_cpt_list_wspe_113808' );

function join_cpt_list_wspe_113808( $query ) 
{
    // If not backend, bail out
    if( !is_admin() )
        return $query;

    // Detect current page and list of CPTs to be shown in Dashboard > invoice > Edit screen
    global $pagenow;
    $cpts = array( 'loads' );

    if( 'edit.php' == $pagenow && ( get_query_var('post_type') && 'invoices' == get_query_var('post_type') ) ){
        //set post type
        $query->set( 'post_type', $cpts );
    
        //Get original meta query
        $meta_query = (array)$query->get('meta_query');

        // show loads that are sent to invoice
        $meta_query[] = array(
                'key'     => 'load_status',
                'value'   => 'complete',
                'compare' => '=',
        );    

        // Set the meta query to the complete, altered query
        $query->set('meta_query',$meta_query);
    }
    return $query;
}

What I want:

On Invoice listing page, column values are not being shown. See screenshot: enter image description here

I am trying this code to get values:

 function invoice_columns ( $columns ) {
     //unset($columns['title']);
     unset($columns['date']);
     
   return array_merge ( $columns, array ( 
     'company' => __ ( 'Company' ),
     'invoice'   => __ ( 'Invoice #' ) ,
     'reference'   => __ ( 'Reference' ) ,
     'delivery_date'   => __ ( 'Delivery Date' ) ,
     'sent_on'   => __ ( 'Sent On' ) ,
     'invoice_date'   => __ ( 'Invoice Date' ) ,
     'due_date'   => __ ( 'Due Date' ) ,
     'invoice_total'   => __ ( 'Invoice Total' ) ,
     'balance'   => __ ( 'Balance' ) 
   ) );
 }
add_filter ( 'manage_invoices_posts_columns', 'invoice_columns' );
 
function invoice_custom_column($column, $post_id){
    echo $column;
    echo $post_id;
    
    
}
add_action ( 'manage_invoices_posts_custom_column', 'invoice_custom_column', 10, 2 );

Please give your solutions.

1
  • I registered a post type invoices, and then used your exact code, and it worked (cln.sh/kHwPTYb0). Have you tried your code on a fresh WordPress install?
    – Caleb
    Jun 9, 2023 at 13:45

1 Answer 1

0

The correct action hook for printing custom column content is manage_loads_posts_custom_column, not manage_invoices_posts_custom_column (tested). This is because the row is itself for a loads post type, and not an invoices post type. Because you're on the page for invoices, the filter to add columns works with invoices.

Tested:

add_action( 'manage_loads_posts_custom_column', 'invoice_custom_column', 10, 2 );

I received an error when setting the post_type parameter to an array, so I set to loads as a string. Additionally, pre_get_posts is an action, not a filter, so the return $query is not needed.

3
  • No @Caleb my action hook is correct. Jun 9, 2023 at 11:31
  • Updated my answer after testing and solution.
    – Caleb
    Jun 9, 2023 at 13:55
  • Ah yes, you almost fixed my problem. Thankyou much. just one more condition was needed to differentiate between the columns of loads and columns of invoice page. I set condition like this - prnt.sc/d9U9aK5YWZKK Jun 9, 2023 at 15:50

Your Answer

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

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