-1

I've created a custom post type via this function:

function custom_post_type() {

    $labels = array(
        'name'                => _x( 'advertising type', 'Post Type General Name', 'twentythirteen' ),
        'singular_name'       => _x( 'advertising type', 'Post Type Singular Name', 'twentythirteen' ),
        'menu_name'           => __( 'advertising type', 'twentythirteen' ),
        'parent_item_colon'   => __( 'Parent advertising', 'twentythirteen' ),
        'all_items'           => __( 'All advertising', 'twentythirteen' ),
        'view_item'           => __( 'View advertising', 'twentythirteen' ),
        'add_new_item'        => __( 'Add New advertising', 'twentythirteen' ),
        'add_new'             => __( 'Add New', 'twentythirteen' ),
        'edit_item'           => __( 'Edit advertising', 'twentythirteen' ),
        'update_item'         => __( 'Update advertising', 'twentythirteen' ),
        'search_items'        => __( 'Search advertising', 'twentythirteen' ),
        'not_found'           => __( 'Not Found', 'twentythirteen' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
        'post_type'      => 'attachment',
    'post_parent'    => $post->ID,
    'post_status'    => 'inherit',
    'post_mime_type' => 'image',
    );


    $args = array(
        'label'               => __( 'advertising', 'twentythirteen' ),
        'description'         => __( 'advertising news and reviews', 'twentythirteen' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),

        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
                'has_archive' => true,
        'taxonomies' => array('category', 'post_tag') ,// this is IMPORTANT

        'capability_type'     => 'page',

    );
        register_taxonomy( 'advertising_type', 'advertising', $args );


    register_post_type( 'advertising', $args );

}



add_action( 'init', 'custom_post_type', 0 );

Which include a taxonomy "advertising_type"

In my loop, I've ad the query post to display only post from a specific taxonomy:

<?php
global $wp_query;
$args = array_merge( $wp_query->query_vars, array('post_type' => 'advertising', 'posts_per_page' => 50, 'advertising_type' => 'recruitment',
 ) );
query_posts( $args );
while(have_posts()): the_post();
?>



        <?php while ( $folio_loop->have_posts() ) : $folio_loop->the_post(); ?>

        <?php endwhile; ?>

which is recruitment using: 'advertising_type' => 'recruitment'

However . . . I don't manage to make it work . . it doesn't display any post . . . If i delete this query I have the post from my custom post type display, so I imagine the problem is really coming from this ( That I can not display post from a specific taxonomy)

Any helps would be amazing,

thank you for your time and help guys

  • 3
    Never use query_posts, search this site for pre_get_posts action. – Milo Jan 14 '15 at 14:41
1

First off, I'd like to note a few things wrong with your Post Type and Taxonomy set up. There's a few settings in your $labels that don't make sense:

$labels = array(
    ...
    'post_type'      => 'attachment',
    'post_parent'    => $post->ID,
    'post_status'    => 'inherit',
    'post_mime_type' => 'image',
);

These last 4 options don't make much sense ( to me anyway ) when creating Post Type labels or Taxonomy labels. There'll never be a $post->ID set in the init function and labels is not the place where you would set a post_type, also attachment is a reserved / built-in post type so you can't reuse it. To view a full list of accepted Labels that you can use, follow the links below:

Post Type Labels

Taxonomy Labels

You'll notice that there's different labels for Taxonomy than there are Post Types. Along with a multitude of other settings, I wouldn't suggest you use the same arguments whenever you register taxonomy and post types but instead create separate arguments for each.

Assuming your Post Type and Taxonomy works, Milo is right ( in the comments section ) you shouldn't use query_posts and Here's Why. If you want you can create a new WP_Query or use pre_get_posts ( as mentioned above ).

Use pre_get_posts To Modify The Main Query

As said in the title, pre_get_posts will modify the main query before it actually gets outputted. This means that you can retain the query_vars that are ( presumably ) already set and you may overwrite anything you need. You would want to put this into your functions.php file, and it works like this:

( Pre Get Posts Codex Article )

function advertising_pgp( $query ) {
    if( ! is_admin() ) {    // Do Not run on Admin Pages

        if( $query->is_main_query() ) {     // Only run on the Main Query

            $query->set( 'post_type', 'advertising' );
            $query->set( 'posts_per_page', 50 );
            $query->set( 'tax_query', array(
                array(
                    'taxonomy'  => 'advertising_type',
                    'field'     => 'slug',
                    'terms'     => 'recruitment'
                )
            ) );

        }
    }
}
add_action( 'pre_get_posts', 'advertising_pgp' );

:: Warning :: The above will modify all main queries on your website. You may need to add additional conditional statements into the second conditional such as is_page( 'Your-Page' )

Create a New WP Query

WP Query is mostly used if you need an additional query to your main query. If you want to just modify your main query to something different, you can use pre_get_posts above, and since you're wanting to combine query_vars - I suggest pre_get_posts since it will already have them in there. I'll leave this here just in case.

global $wp_query;

$new_query = new WP_Query(
    array_merge( 
        $wp_query->query_vars,
        array(
            'post_type' => 'advertising',
            'posts_per_page' => 50, 
            'advertising_type' => 'recruitment'
        )
    )
);

<?php if( $new_query->have_posts() ) : ?>

    <?php while( $new_query->have_posts() ) : $new_query->the_post(); ?>

        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>

    <?php endif; ?>

<?php endif; ?>

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.