WordPress.org

Ready to get started?Download WordPress

Forums

Constructing a WP_Query (6 posts)

  1. Maraki
    Member
    Posted 1 year ago #

    Hi there,

    I am a newbie when it comes to coding and I want to construct a WP_Query based on the codex.

    https://codex.wordpress.org/Class_Reference/WP_Query

    The arguments are:

    - 5 posts
    - from custom post type "os_calendar"
    - in taxonomy called "formula1"
    - showing the thumbnail (preferably my defined custom thumbnail size called "spotlight") & post title in a list .li. format

    Your help is much appreciated!

  2. Justin
    Member
    Posted 1 year ago #

    Something like the following ( may not be completely accurate ). For the thumbnail you could run get_post_meta() or better. Visit http://codex.wordpress.org/Post_Thumbnails for documentation for thumbnails

    $args = array(
    	'post_type' => 'os_calendar',
    	'taxonomy' => 'formula1',
            'post_per_page' => 5
    );
    $query = new WP_Query( $args );
  3. Maraki
    Member
    Posted 1 year ago #

    ok that looks good but i probably need some more help as i am getting confused with the image placement!

  4. Maraki
    Member
    Posted 1 year ago #

    <?php
    
    $args = array(
    'post_type' => 'os_calendar',
    'taxonomy' => 'calendar_event_type',
    'post_per_page' => 5);
    $recentPosts = new WP_Query( $args );
    $recentPosts->query('showposts=5');
    while ($recentPosts->have_posts()) : $recentPosts->the_post();
    ?>
    <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    
    </div>
    
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>

    I came up with the above to start with but for some reason i am getting blank results :(

  5. racer x
    Member
    Posted 1 year ago #

    You don't need this line at all:
    $recentPosts->query('showposts=5');
    You have already limited the posts to 5 in the args.

    You may want to look at the taxonomy section again in the codex. Are you looking for a specific term from that taxonomy?

    To get the post thumbnail at your custom size:
    <?php echo get_the_post_thumbnail( $post->ID, 'spotlight' ); ?>

    Hope that helps a little.

  6. Maraki
    Member
    Posted 1 year ago #

    Yes it does.. I also got this reply from my theme editor which i think messes up my code..

    Yes, the pre_get_posts filter that is used in functions/custom.php lines 23 - 40 alters the query. You should add suppress_filters to your query.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.