0

I am not a php coder, I am a designer and html and css guy. However, I am stuck at the moment trying to write a piece of code that will find the name of a post, then get and display all of the posts that have that name as their category. Something like this,

Agent bio page has info about the agent and the bio post is his name and cat name. He has listings under his name which is also his category in the wp backend under categories. So under the bio info, I need to create a function that will display a list of all of his posts and display them with the thumbnail, some excerpt text and a couple of the custom fields I have created.

this will be on a agent-single.php that will be used for every agent i have so it needs to find the post name with his slug in it and find posts based on that. Is there any way to actually do this or am I hoping for something that can not be done in wp?

  $template = basename(get_permalink());  // get agent here
  $taskarr = array (
    'post_type'   => 'listing',
    'post_status' => 'publish',
    'category' => $template,
    'order'       => 'ASC',
    'orderby'     => 'meta_value_num',
    'metakey'     => 'listing_date', // this is a custom field
    'metakey'     => 'location',
  );
  $tasks = get_posts($taskarr);
  foreach( $tasks as $task ) {
      //  Do stuff here, for example:
      echo '<li>';
      echo $task->post_title;
      echo $task->the_excerpt;
      echo $task->location;
          echo '</li>';
  }

this was inside my main loop which was a normal loop and of course it mutilates my code! ugh!!!

  • This can be done, but you need to show us some actual code or what you have tried. – Wyck Jun 28 '13 at 16:15
  • How is this different from your other question, "list posts of a person by a variable?"? – vancoder Jun 28 '13 at 16:17
  • possible duplicate of list posts of a person by a variable? – Wyck Jun 28 '13 at 16:17
  • because I am not sure if I have been explaining this correctly, I have been working on it for 4 weeks to no avail. I have asked around the wp.org forums and no one can help me, I have tried everything under the sun to make it work and I am really no so sure what code I should put here as an example because none of them work and I am just simply frustrated and seeing red for the last 2 days, lol. Not to be a butt, but I have literally been up for the last 39 hours working on this and I cant even see my keys on my keyboard anymore, just blurry stuff all over the place, lol. – James G Jun 28 '13 at 16:21
  • As of the last 13 hours I have simply been grasping at straws for any piece of code to get this to work, my last one I will post in the question above now. – James G Jun 28 '13 at 16:22
0

Using get_posts():

 <?php
      $category = get_category_by_slug($post->post_name);
      $template =  $category->term_id;
      $taskarr = array(
           'post_type' => 'listing',
           'post_status' => 'publish',
           'category' => $template,
           'order' => 'ASC',
           'orderby' => 'meta_value_num',
           'meta_key' => 'listing_date'
      );
      $tasks = get_posts($taskarr);
      foreach( $tasks as $task ) {
           echo '<li>';
           echo $task->post_title;
           echo $task->post_excerpt;
           echo get_post_meta( $task->ID, 'location', 'single');
           echo '<li>';
      }
 ?>

Using WP_Query:

 <?php
       $catSlug = $post->post_name;
       $args = array(
           'post_type' => 'listing',
           'post_status' => 'publish',
           'category_name' => $catSlug,
           'order' => 'ASC',
           'orderby' => 'meta_value_num',
           'meta_key' => 'listing_date'

       );
       $the_query = new WP_Query( $args );
       while ( $the_query->have_posts() ) {
           $the_query->the_post();
           echo '<li>';
           the_title();
           the_excerpt();
           echo get_post_meta( get_the_ID(), 'location', 'single');
           echo '<li>';
  }
  wp_reset_postdata();

?>

  • looks very nice and simple, will this need to go int my main loop or will I have to wp_query in a multiple loop? – James G Jun 28 '13 at 16:35
  • I updated it to match your query arguments. I removed the extra meta key as you can only have one unless you do a meta_query – eteich Jun 28 '13 at 16:40
  • metakey --> meta_key ? – birgire Jun 28 '13 at 16:44
  • whoops... edited – eteich Jun 28 '13 at 16:48
  • Hey Thanks Eteich, I tried it and it simply made my page go blank, here is how I added your code, well I will post it as an answer I guess, here goes – James G Jun 28 '13 at 16:54

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.