• Resolved csleh

    (@csleh)


    My goal is to grab a list of posts in a custom post type that are also child of category 4, then have a link that goes to a category page — the category has the same name as the title being pulled.

    This code works great for the first part, getting the list of posts:

    <?php $args=array('category_name' => 'mycategoryname' , 'post_type' => 'mycustomposttypename',);
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <h1><?php the_title(); ?></h1>
        <?php  endwhile; }
    wp_reset_query(); ?>

    Now, if I could refine the query further, I’m trying to find the permalink to a category with a name identical to what is shown in the_title. I thought this might help:

    <?php
       $pageslug = $post->post_name; // name of the page (slug)
       $catslug = get_category_by_slug($pageslug); // get category of the same slug
       $catid = $catslug->term_id;  // get id of this category
       $query= 'cat=' . $catid. '';
       query_posts($query); // run the query
    ?>

    But combining these two seems to be beyond me as I keep breaking everything.

    If this is the wrong approach feel free to suggest alternatives.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Depending on your permalink structure, you may be able to do something like this (replace mysite.com with your site name):

    <?php $args=array('category_name' => 'mycategoryname' , 'post_type' => 'mycustomposttypename',);
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <a href="http://mysite.com/category/<?php echo $post->post_name; ?>/" ><?php the_title(); ?></a>
        <?php  endwhile; }
    wp_reset_query(); ?>
    Thread Starter csleh

    (@csleh)

    For reasons beyond my control, this site is going to be converted to static html then moved to the final hosting area (please don’t ask, I don’t even know).

    This approach though is very interesting and creative, so I’m going to see if I can it work with relative url.
    Thank you!

    Thread Starter csleh

    (@csleh)

    vtxyzzy, you are a genius. I changed my link to this:
    <a href="<?php echo get_option('home'); ?>/index.php/category/time-and-attendance/<?php echo $post->post_name; ?>" >Read more on <?php the_title(); ?></a>
    I’ll need to remember to edit this once I figure out how to get the “index.php” and “category” out of the url (windows server).

    Note to anyone else trying this: my version here only works because I have a post in a custom post type that has the exact same name as a category — but the custom post type post is never viewed on it’s own.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘can these be combined in a query?’ is closed to new replies.