WordPress.org

Forums

My Post Order
Troubles with WP_Query object (5 posts)

  1. cfetr
    Member
    Posted 2 years ago #

    Greetings, I am having troubles getting posts to sort using the WP_Query object. It works fine using query_posts('section_name=campaigns');.

    I have tried:

    $post_query = new WP_Query('section_name=test');
    $post_query = new WP_Query(array('section_name'=>'test');

    The site is not open to the web so I can't link for you. Thanks for your help and your work.

    http://wordpress.org/extend/plugins/my-posts-order/

  2. cfetr
    Member
    Posted 2 years ago #

    Edit - I am aware that there is a missing closing parenthesis. That is only a typo in this post.

  3. Kapil Chugh
    Member
    Plugin Author

    Posted 2 years ago #

    Can you please paste complete index.php.

    If it is working with query_posts then it must work with WP_Query.

  4. cfetr
    Member
    Posted 2 years ago #

    Thanks for your help. I am sure it is something simple that I am missing since I can make query_posts() work.

    <?php
    global $post;
    
    $postQuery = new WP_Query(array('section_name'=>'campaigns'));
    
    get_header(); ?>
    
    	<div id="primary" class="site-content">
    		<div id="content" role="main">
        	<div id="campaigns" class="page-content">
          	<h2 class="page-title"><?php the_title(); ?></h2>
    				<?php if ( $postQuery->have_posts() ) : ?>
            <?php /* Start the Loop */ ?>
              <?php while ( $postQuery->have_posts() ) : $postQuery->the_post(); ?>
              <?php //get_template_part( 'content', get_post_format() ); ?>
              <div id="post-<?php echo $post->ID; ?>" class="post">
                <h3 class="post-title"><?php the_title(); ?></h3>
                <div class="post-content">
                  <?php the_content(); ?>
                </div>
              </div>
              <?php endwhile; //end the loop ?>
            <?php else : ?>
              <p>There are no posts to display.</p>
            <?php endif; ?>
          </div><!-- #campaigns -->
    		</div><!-- #content -->
    	</div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  5. Kapil Chugh
    Member
    Plugin Author

    Posted 2 years ago #

    Try this code:

    <?php
    get_header(); ?>
    
      <div id="primary" class="site-content">
        <div id="content" role="main">
          <div id="campaigns" class="page-content"> <?php
            $postQuery = new WP_Query(array('section_name'=>'campaigns'));
            if ( $postQuery->have_posts() ) : ?>
            <?php /* Start the Loop */ ?>
              <?php while ( $postQuery->have_posts() ) : $postQuery->the_post(); ?>
              <?php //get_template_part( 'content', get_post_format() ); ?>
              <div id="post-<?php echo get_the_ID(); ?>" class="post">
                <h3 class="post-title"><?php the_title(); ?></h3>
                <div class="post-content">
                  <?php the_content(); ?>
                </div>
              </div>
              <?php endwhile;
              wp_reset_postdata(); //end the loop ?>
            <?php else : ?>
              <p>There are no posts to display.</p>
            <?php endif; ?>
          </div><!-- #campaigns -->
        </div><!-- #content -->
      </div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    I haven't tested it, so there may be some syntax error. Please check and revert back.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • My Post Order
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.