how to get posts by custom post type then display Custom fields?

I have custom posts types for Recipes, and within that I have set up Advanced Custom Fields (from the plugin.)

Within, that custom post type, I have several categories. Two questions:

1.) How do I get all posts from a custom category? I get how to display the custom fields, but not sure how to call all the posts from a custom category, then I can display it’s title, image, and link.

2.) In a custom post type scenario am I better off setting up custom categories as well or use the generic post categories?

I’ve tried the get_posts, then tried by getting posts by custom category. for example

<?php
$args = array(
     'posts_per_page' => 8,
     'orderby' => 'rand',
     'post_type' => 'recipes',
     'type' => 'side-dishes',
     'post_status' => 'publish'
);
$show_albums = get_posts( $args );
?>

I don’t know the exact implementation, since post_type is a custom post, and figure I would have to do some for-each probably to then use the_field (from ACF)

===========================================================
Ok after taking suggestion below here is what I have please let me know if I am doing this correct?

here is the query – which seems to work:

<?php

// The Query
$the_query = new WP_Query($args = array(
    'post_type' => 'recipes',
    'custom_cat' => 'side-dishes'
) );

// The Loop
if ( $the_query->have_posts() ) {
        echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
        echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();?>

I registered the taxonomy, but what’s confusing me is that, my registered post-type is recipes, shouldn’t register_taxonomy be “recipes” and not ‘custom_cat?”

register_taxonomy( 'custom_cat', 
        array('recipes'), /* if you change the name of register_post_type( 'custom_type', then you have to change this */
        array('hierarchical' => true,     /* if this is true, it acts like categories */
            'labels' => array(
                'name' => __( 'Recipe Categories', 'bonestheme' ), /* name of the custom taxonomy */
                'singular_name' => __( 'Recipe Category', 'bonestheme' ), /* single taxonomy name */
                'search_items' =>  __( 'Search Recipe Categories', 'bonestheme' ), /* search title for taxomony */
                'all_items' => __( 'All Recipe Categories', 'bonestheme' ), /* all title for taxonomies */
                'parent_item' => __( 'Parent Recipe Category', 'bonestheme' ), /* parent title for taxonomy */
                'parent_item_colon' => __( 'Parent Custom Category:', 'bonestheme' ), /* parent taxonomy title */
                'edit_item' => __( 'Edit Custom Category', 'bonestheme' ), /* edit custom taxonomy title */
                'update_item' => __( 'Update Custom Category', 'bonestheme' ), /* update title for taxonomy */
                'add_new_item' => __( 'Add New Custom Category', 'bonestheme' ), /* add new title for taxonomy */
                'new_item_name' => __( 'New Custom Category Name', 'bonestheme' ) /* name title for taxonomy */
            ),
            'show_admin_column' => true, 
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => array( 'slug' => 'custom-slug' ),
        )
    );

here is the registered post type:

function create_recipe() { 
    // creating (registering) the custom type 
    register_post_type( 'recipes', /* (http://codex.wordpress.org/Function_Reference/register_post_type) */
        // let's now add all the options for this post type
        array( 'labels' => array(
            'name' => __( 'Recipes', 'bonestheme' ), /* This is the Title of the Group */
            'singular_name' => __( 'Recipe', 'bonestheme' ), /* This is the individual type */
            'all_items' => __( 'All Recipes', 'bonestheme' ), /* the all items menu item */
            'add_new' => __( 'Add New Recipe', 'bonestheme' ), /* The add new menu item */
            'add_new_item' => __( 'Add Recipe', 'bonestheme' ), /* Add New Display Title */
            'edit' => __( 'Edit', 'bonestheme' ), /* Edit Dialog */
            'edit_item' => __( 'Edit Recipe', 'bonestheme' ), /* Edit Display Title */
            'new_item' => __( 'New Recipe Type', 'bonestheme' ), /* New Display Title */
            'view_item' => __( 'View Recipe', 'bonestheme' ), /* View Display Title */
            'search_items' => __( 'Search Recipes', 'bonestheme' ), /* Search Custom Type Title */ 
            'not_found' =>  __( 'Nothing found in the Database.', 'bonestheme' ), /* This displays if there are no entries yet */ 
            'not_found_in_trash' => __( 'Nothing found in Trash', 'bonestheme' ), /* This displays if there is nothing in the trash */
            'parent_item_colon' => ''
            ), /* end of arrays */
            'description' => __( "Recipes section for It's Just food", 'bonestheme' ), /* Custom Type Description */
            'public' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'show_ui' => true,
            'query_var' => true,
            'menu_position' => 4, /* this is what order you want it to appear in on the left hand side menu */ 
            'menu_icon' => get_stylesheet_directory_uri() . '/library/images/custom-post-icon.png', /* the icon for the custom post type menu */
            'rewrite'   => array( 'slug' => 'recipes', 'with_front' => false ), /* you can specify its url slug */
            'has_archive' => 'recipes', /* you can rename the slug here */
            'capability_type' => 'post',
            'hierarchical' => false,
            /* the next one is important, it tells what's enabled in the post editor */
            'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'sticky')
        ) /* end of options */
    ); /* end of register post type */

1
1

This might be a good read for you. I’m not sure exactly what you’re looking to do so I’ll lay this out in a simple format.

This query will pull all Posts under the Post Type recipes where the custom field side-dishes exists. You can then loop through and display them how you would like.

$recipes = new WP_Query(
    array(
        'post_type' => 'recipes',
        'posts_per_page' => 8,
        'orderby' => 'rand',
        'post_status' => 'publish',
        'meta_query' => array(
            array(
                'key' => 'side-dishes'
                'compare' => 'EXISTS'
            )
        )
    )
);

This 2nd query will pull all posts under the taxonomy my_taxonomy_name_here which you will need to replace with your taxonomy. It looks for categories with the slug my_category_slug_here which you will need to replace with the category slug. You can replace ‘slug’ with ID and pull it based off that if you’d like.

$recipes = new WP_Query(
    array(
        'post_type' => 'recipes',
        'posts_per_page' => 8,
        'orderby' => 'rand',
        'post_status' => 'publish',
        'tax_query' => array(
            array(
                'taxonomy' => 'my_taxonomy_name_here',
                'field' => 'slug',
                'terms' => 'my_category_slug_here'
            )
        )
    )
);

You can then loop through each post like a normal loop:

<?php if($recipes->have_posts()) : ?>
    <?php while($recipes->have_posts()) : $recipes->the_post(); ?>
        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>
    <?php endwhile; ?>
<?php endif; wp_reset_query(); ?>

Documentation on WP_Query

Documentation on Meta_Query

Documentation on Tax_Query

Leave a Comment