WordPress.org

Ready to get started?Download WordPress

Forums

Sorting Pages by Category (4 posts)

  1. Devin Price
    Member
    Posted 4 years ago #

    I am looking to sort my pages by category. I am using the Misiek Page Category plug-in to put categories on pages, but I don't know the correct syntax to call the categories.

    I would like the template to do something like the following, but obviously cat won't work here:

    <?php if (is_page('kayaking-trips')) {
      <?php query_posts("showposts=4&cat='kayak'"); ?>
        <?php while (have_posts()) : the_post(); ?>
          <?php the_title(); ?>">
         <?php endwhile; ?>
      <?php endif; ?>

    Ideas?

  2. MichaelH
    Member
    Posted 4 years ago #

    Find the id for your "kayak" mpc_categories id and use that in this page template:

    <?php
    /*
    Template Name: MPCbasedPage
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="content" class="narrowcolumn">
    
    <?php
    if (function_exists('mpc_get_categories')) {
      $mpc_cats=MPC_CATEGORIES;
      $mpc_page_cats=MPC_PAGES_CATEGORIES;
    
      $querystr= "SELECT post_id FROM $mpc_page_cats
        WHERE $mpc_page_cats.category_id IN ('19') //replace 19 with your mpc category id/ids '1,2,19'
        LIMIT 0,4";
    
      $catpageids = $wpdb->get_col($querystr,0);
    
      if ($catpageids) {
        $args = array(
          'post__in' => $catpageids,
    	    'post_type' => 'page'
    	    );
        $pageposts = get_posts($args);
      }
    }
    ?>
    
    <?php if ($pageposts): ?>
      <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post); ?>
    
        <div class="post" id="post-<?php the_ID(); ?>">
          <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
          <?php the_title(); ?></a></h2>
          <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
          <div class="entry">
             <?php the_content('Read the rest of this entry »'); ?>
          </div>
    
    				<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
        </div>
      <?php endforeach; ?>
    
      <?php else : ?>
        <h2 class="center">Not Found</h2>
        <p class="center">Sorry, but you are looking for something that isn't here.</p>
        <?php //include (TEMPLATEPATH . "/searchform.php"); ?>
     <?php endif; ?>
    
    </div>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    Page template works with 2.8.4 and the WordPress Default theme with MPC plugin activated.

  3. kyle_barber
    Member
    Posted 4 years ago #

    Is there an easy way to determine the mpc_categories id number?

  4. Devin Price
    Member
    Posted 4 years ago #

    Thanks for the amazing support on this MichaelH. It was more than expected and fully appreciated.

    I explored several more plug-ins and decided Flutter might be a better approach for what I was wanting to do (and simpler for a novice coder). But for Kyle, and anyone else looking for a solution to this problem, try this code:

    <?php if (is_page('your-page-to-display-cat-id-2')) { ?>
    
    <?php
    if (function_exists('mpc_get_categories')) {
      $mpc_cats=MPC_CATEGORIES;
      $mpc_page_cats=MPC_PAGES_CATEGORIES;
    
      $cats = mpc_get_categories();
    
    //Will display a list of the categories so you know which ids they have
      echo '<!--';
      print_r($cats);
      echo '-->';
    
      /*
      SELECT post_id FROM wp_mpc_pages_categories
      WHERE category_id IN ('4')
      LIMIT 0,4
      */
    
      //get category id
      //This uses 2 because I want posts from cat 2 to show on this page
      $category_id = 2;
    
      //build query
      $querystr = sprintf("SELECT post_id FROM %s
        		   		   WHERE category_id IN ('%s')
        		   		   LIMIT 0,4", $mpc_page_cats, $category_id);
    
      //get result
      $catpageids = $wpdb->get_col($querystr,0);
    
      var_dump($catpageids);
    
      //do we have a result?
      if ( $catpageids ) {
    
      	// let's assume $catpageids is an array
    	$pageposts = get_pages(array(
    		'include' => implode(',', $catpageids)
    	));
    
      	/*
        $args = array(
          'post_in' => $catpageids,
    	  'post_type' => 'page'
    	);
    
    	//http://codex.wordpress.org/Template_Tags/get_posts
        $pageposts = get_posts($args);
    	*/
      }
    }
    ?>
    
    <?php if ($pageposts): ?>
      <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post); ?>
    
        <div class="post" id="post-<?php the_ID(); ?>">
          <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
          <?php the_title(); ?></a></h2>
          <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
          <div class="entry">
             <?php the_content('Read the rest of this entry »'); ?>
          </div>
    
    				<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
        </div>
      <?php endforeach; ?>
    
      <?php else : ?>
        <h2 class="center">Not Found</h2>
        <p class="center">Sorry, but you are looking for something that isn't here.</p>
        <?php //include (TEMPLATEPATH . "/searchform.php"); ?>
     <?php endif; ?>
    
    <?php } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic