Search code examples
phpwordpresscustom-post-type

Wordpress Query Posts from specific subcategory of Custom Post Type


Okay guys, this one has me somewhere between ripping my hair out and kicking in my monitor. It seems no matter what I try, no matter how many times I re-write the query, change terms, change syntax...I get nothing. So I have a website I am working on where we have a custom post type of 'Vinyl' for vinyl records of in an online collection the client wants displayed alphabetically, with a category of 'vinyls' inside the custom post type section. The client then further wanted to separate things down and create a child category within 'vinyls' called 'vinyl_ae' (Vinyls alphabetically sorted by the first letter A through E). Now, I have the issue where I am trying to query any post at all from the vinyl_ae category/sub-category/whatever the hell it is at this point, and NOTHING turns out. The only way I get any results at all is to set an else conditional for the if have_posts() statement. I'll try to include any all data I can here to help sort this mess out.

  1. Custom post type - name : vinyl
  2. main category - name : VINYLs, slug : vinyls, ID : 3
  3. child category - name : Vinyl A-E, slug : vinyl_ae, ID : 4571

Screen Shots of all my category and sub-category layouts

Here is my working Code Currently (take in mind I have stripped it down so much tonight there is not much left and I have tried so many different solutions to the point where I am practically copying and pasting based on results others are having)

<section id="main">
<div class="content-holder no-spacing">
    <div class="container">
        <div class="content-inner">
            <div id="ajax" class="records row">

            <?php
            $args = array(
            'post_type' => 'post' ,
            'posts_per_page' => 6,
            'cat'         => '4571',
            'paged' => get_query_var('paged'),
            'post_parent' => $parent); 
            $mv = new WP_Query($args);
            if ( $mv->have_posts() ) { 

            while ( $q->have_posts() ) {

            $serial = get_field('serial');
            $mv->the_post(); ?>

            <div class="serial-num"><?php echo $serial; ?></div>

            <?php } ?>

            <?php } else { ?>
            <em>Things Still Screwy</em>
            <?php } ?>

        </div>
    </div>
</div>

Thank any and all in advance for any help that can be given, I've thrown in the towel here.


Solution

  • There is two things that you should consider in your sample code:

    1. You should set post_type argument to your custom post type vinyl not the post post type.

    2. If the serial is a field of your post you should call the $mv->the_post(); line first and after that call the $serial = get_field('serial'); statement, because before the_post() call, you can't access post's meta data.