This topic has 1 reply, 2 voices, and was last updated 8 years by sharmstr.

  • Author
  • #80817
     Klame
    Participant

    It dont work for me…
    I have enabled the “Enable custom posts related” under theme options -> blog.

    But don’t get anything on my single-cpt.php page.

    I came across this code on page-parts/post-related

    /* Query args */
    $args=array(
    ‘post__not_in’ => array($post->ID),
    ‘showposts’=> 8,
    ‘orderby’ => ‘rand’ //random posts
    );

    //logic for blog posts
    if (is_singular(‘post’)) {

    //related text
    $related_text = __(“Related Articles”, “kleo_framework”);

    $categories = get_the_category($post->ID);

    if (!empty($categories)) {
    $category_ids = array();
    foreach ($categories as $rcat) {
    $category_ids[] = $rcat->term_id;
    }

    $args[‘category__in’] = $category_ids;
    }
    }
    // logic for custom post types
    else {

    //related text
    $related_text = __(“Related”, “kleo_framework”);

    global $post;
    $categories = get_object_taxonomies($post);

    if (!empty($categories)) {
    foreach( $categories as $tax ) {
    $terms = wp_get_object_terms($post->ID, $tax, array(‘fields’ => ‘ids’));

    $args[‘tax_query’][] = array(
    ‘taxonomy’ => $tax,
    ‘field’ => ‘id’,
    ‘terms’ => $terms
    );
    }
    }
    }

    And thought “Hey, I should definitely do something about this!
    Probably this line: $categories = get_the_category($post->ID);
    You sir, are a GENIUS!

    But then I realised.. I’m not a genius!
    I dont know what to?
    What does $post->ID tells you?

    My custom post type is made by this plugin:
    https://wordpress.org/plugins/events-manager/

    It is using codes like this:
    echo $EM_Event->output(‘#_CATEGORYNEXTEVENT’);
    To get related post (But they are not very pretty, and for me, not easy to style)

    If that makes any difference. 🙂

    Thanks

    #80946
     sharmstr
    Moderator

    According to the plugin, you cant use query_posts to get the events. That’s what the related code uses. You can test this yourself by changing

    COPY CODE
    
    query_posts($args);
    
    COPY CODE
    
    query_posts("post_type=event");
    

    What that *should* do is return all events regardless of event_categories and event_tags. You’ll see that it doesnt.

    You’ll have to use WP_Query instead. There’s a ton of info on their site about how to use it.

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution

    This support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com

Viewing 2 posts - 1 through 2 (of 2 total)

The forum ‘KLEO’ is closed to new topics and replies.

Log in with your credentials

Forgot your details?