2

On my post page I am trying to display list of other posts from the same category of the original post. So far I got this and this does not seem to work:

<?php

    $args = array(
        'post_type' => 'article',
        'posts_per_page' => 5,
        'post__not_in' => array( get_the_ID() ),
        'category'     => array( get_the_category() ),
        'meta_query' => array(
                    array(
                    'key' => 'recommended_article',
                    'value' => '1',
                    'compare' => '=='
                        )
                    )
    );
    $query = new WP_Query( $args );

?>

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

<a class="popup-article-picture" href="<?php the_permalink(); ?>" style="background-image: url('<?php the_post_thumbnail_url(); ?>');"></a>
<a class="popup-article-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

    <?php endwhile; endif; wp_reset_postdata(); ?>
  • Is this within the loop for the main post? Is the built-in taxonomy category attached to your custom post type? Are there any articles in the same category with meta value recommended_article == 1? When you say it doesn't work, what are you expecting to see and how do you judge that it's not working as it should? – Andy Macaulay-Brook Sep 21 '16 at 14:59
  • @Andy This loop is within template page for custom post type article. recommended_article == 1 allows me to filter posts that I mark recommended with true/false thingy from ACF. – ERDFX Sep 21 '16 at 15:05
  • There isn't anywhere near enough information to help you. Can you show more of the template file concerned? Can you show where you have attached Categories to the CPT article? – Andy Macaulay-Brook Sep 21 '16 at 15:08
  • @Andy, i used Custom Post Types UI to create custom post type and attach it to exisiting WP categories, the loop is inside single-article.php in my template. I am very new at PHP and wordpress so that about as much as I can tell really. – ERDFX Sep 21 '16 at 15:12
  • Thanks - just eliminating possible basic problems. I think we need to see the whole template, if you can edit your question to include it. – Andy Macaulay-Brook Sep 21 '16 at 15:13
2

I've found an answer:

<?php

    $cats = get_the_category();
    $args = array(
        'post_type' => 'article',
        'post__not_in' => array( get_the_ID() ),
        'posts_per_page' => 5,
        'cat'     => $cats[0]->term_id,
        'meta_query' => array(
                    array(
                    'key' => 'recommended_article',
                    'value' => '1',
                    'compare' => '=='
                        )
                    )
    );
    $query = new WP_Query( $args );

?>  

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

<!--HTML-->

<?php endwhile; endif; wp_reset_postdata(); ?>
  • not working now :( – Shiv Singh Mar 15 at 9:34
1

you are trying to query the custom post type called article. are you using default WordPress post categories for post type article? or have registered any custom taxonomy for that post type? i assume you are using default WordPress category for CPT. so first step is to get the current category from single page. following function will return categories attached to the post from outside the loop.

get_the_category();

it will return an array of term objects. and you have to get the slug from this array to pass in the query. lets assume we have only one category assigned for single post.

$category_obj = get_the_category();
$category = $category_obj[0]->slug;

now you can use that in your related posts query.

$args = array(
        'post_type' => 'article',
        'posts_per_page' => 5,
        'category'     => $category,
        'meta_query' => array(
                    array(
                    'key' => 'recommended_article',
                    'value' => '1',
                    'compare' => '=='
                        )
                    )
    );
    $query = new WP_Query( $args );

and if you are using custom taxonomy for post type then let us know so we can help you regarding custom taxonomies.

  • Would you mind explaining what exactly do i need to with that piece of code? Simply adding it to the template didn't work for me. Thank you! – ERDFX Sep 21 '16 at 18:19

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.