wp query - How can I fetch all the dates from custom fields from various different custom post types and showlist them at one pl

I am using wordpress and pods for building a web app.I have created different custom post types like "project"

I am using wordpress and pods for building a web app. I have created different custom post types like "project" , "task" and "material" and each of them have various different custom date fields.

I want to list all of those dates from all of these custom post types at one place in ascending order.

I have so far tried wp_query, get_post_meta and also pods inbuilt query functions but couldnt get any of that to work.

Please help me with this one, thank.

I am using wordpress and pods for building a web app. I have created different custom post types like "project" , "task" and "material" and each of them have various different custom date fields.

I want to list all of those dates from all of these custom post types at one place in ascending order.

I have so far tried wp_query, get_post_meta and also pods inbuilt query functions but couldnt get any of that to work.

Please help me with this one, thank.

Share Improve this question asked Sep 26, 2019 at 5:17 Graham MorteimerGraham Morteimer 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

get_post() and get_post_meta() should work to collect the data and array_multisort() should be able to sort it into ascending order.

    // Query the database to get the posts from your post types

    $project_args = array( 'post_type' => 'projects', "numberposts" => -1 );
    $task_args = array( 'post_type' => 'tasks', "numberposts" => -1 );
    $material_args = array( 'post_type' => 'materials', "numberposts" => -1 );

    $projects = get_posts( $project_args );
    $tasks = get_posts( $task_args );
    $materials = get_posts( $material_args );

    $list_items = array();
    $timestamps = array();

    // Loop through each set of posts adding the data you want to an array and their timestamp to another array
    foreach($projects as $project){ 
        array_push( $list_items, array( 
            'name' => $project->post_title, 
            'date' => get_post_meta( $project->ID, 'custom_date'),
            )
        ); 
        array_push( $timestamps, strtotime( get_post_meta( $project->ID, 'custom_date') ) );    
        }

    foreach($tasks as $task){ 
        array_push( $list_items, array( 
            'name' => $task-> post_title, 
            'date' => get_post_meta( $task->ID, 'custom_date'),
            )
        ); 
        array_push( $timestamps, strtotime( get_post_meta( $task->ID, 'custom_date') ) );       
        }

    foreach($materials as $material){ 
        array_push( $list_items, array( 
            'name' => $material-> post_title, 
            'date' => get_post_meta( $material->ID, 'custom_date'), 
            )   
        );
        array_push( $timestamps, strtotime( get_post_meta( $material->ID, 'custom_date') ) );       
        }

    //Sort both arrays based on the timestamp   
    $array_multisort( $timestamps, $list_items );

    //Output as you like
    print_r( $list_items );

For multiple dates, just add additional meta fields to the foreach loops and be sure to assign the date you want to sort by to the $timestamps variable.

    foreach($materials as $material){ 
        array_push( $list_items, array( 
            'name' => $material-> post_title, 
            'sort_date' => get_post_meta( $material->ID, 'sort_date'),
            'date_1' => get_post_meta( $material->ID, 'date_1'),
            'date_2' => get_post_meta( $material->ID, 'date_2'),
            'date_3' => get_post_meta( $material->ID, 'date_3'), 
            )   
        );
        array_push( $timestamps, strtotime( get_post_meta( $material->ID, 'sort_date') ) );     
        }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745125841a4612685.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信