1

I have a CPT (homepage) and a taxonomy for this CPT that has some fields (about, services ,...) I want to display the posts (and the contents) of each category (of custom taxonomy) in my homepage. but I don't know how do it! Could you please Help me? any helps would be appreciated.

in functions.php:

////////////////////////////////
// CPT - Home Page Content
////////////////////////////////

function cpt_homepage(){
    $labels = array(
        'name'                => 'Home Page Contents',
        'singular_name'       => 'Home Page Content',
        'menu_name'           => 'Home Page Contents'
        'parent_item_colon'   =>  'Parent Home Page Content',
        'all_items'           => 'All Home Page Contents',
        'view_item'           => 'View Home Page Content',
        'add_new_item'        =>'Add New Home Page Content',
        'add_new'             => 'Add New', 
        'edit_item'           =>  'Edit Home Page Content',
        'update_item'         => 'Update Home Page Content',
        'search_items'        =>  'Search Home Page Contents', 
        'not_found'           =>  'Not found',
        'not_found_in_trash'  =>'Not found in Trash',
    );
    $args = array(
        'label'               =>'Home Page Contents',
        'description'         => 'Different Home Page Content, our team may design and develop',
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'thumbnail' ),
        'taxonomies'          => array( 'post_tag' ),
        'hierarchical'        => false,
        'public'              => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 2,
        'menu_icon'           => 'dashicons-welcome-view-site',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
    );
    register_post_type( 'homepage', $args );
}
add_action( 'init', 'cpt_homepage', 0 );

and In home.php (i wanna somthing like query on taxonomy)

  • Please keep in mind, Don't reveal private aspects of your code in public. Its better to simplify your code and then share it. Also make your question more clear as even I could not understand whats your problem and the desired output? – Pmpr Jul 16 '15 at 6:22
0

This should works.

$args = array(
    'taxonomy' => 'your_taxonmy_name', // replace with your taxonomy name
);

$categories = get_categories( $args );

$ids = array(); 

// lets get all post IDs
foreach ( $categories as $category ) {
    $args = array(
        'cat' => $category->term_id,
        'post_type' => 'homepage', // your post type
        'posts_per_page' => '1',
        'post__not_in' => $ids, // don't want to any duplication
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        $query->the_post();

        $ids[] = get_the_ID();
    }
}
wp_reset_postdata();

$args = array(
    'post__in' => $ids,
);

$query = new WP_Query( $args );

// your loop for echoing
if ( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();

        // do what you want to
    }
}
wp_reset_postdata();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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