Home > Back-end >  Fetch custom post instead of default Wordpress posts
Fetch custom post instead of default Wordpress posts

Time:11-01

I am using a plugin in which default wordpress posts are getting displayed on 'posts' tab, this code is fetching the posts, I am looking to fetch custom post type 'properties' instead of default wordpress post.

loop-post.php:

   <?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

$the_query = isset( $args['template_args']['the_query'] ) ? $args['template_args']['the_query'] : '';
$title = isset( $args['template_args']['title'] ) ? $args['template_args']['title'] : '';
?>
<h3><?php echo $title; ?></h3>
<div class="uwp-profile-item-block">
    <?php
    // The Loop
    if ($the_query && $the_query->have_posts()) {

        echo '<ul >';
        while ($the_query->have_posts()) {
            $the_query->the_post();
            uwp_get_template('posts-post.php', $args);
        }
        echo '</ul>';

        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
        // no posts found
        echo "<p>".sprintf( __( "No %s found.", 'userswp' ), $title )."</p>";
    }
    do_action('uwp_profile_pagination', $the_query->max_num_pages);
    ?>
</div>

posts-post.php:

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
global $post;
$user = uwp_get_displayed_user();
?>
<li class="uwp-profile-item-li uwp-profile-item-clearfix">
    <div class="uwp_generic_thumb_wrap">
        <a class="uwp-profile-item-img" href="<?php echo esc_url_raw( get_the_permalink() ); ?>">
            <?php
            if ( has_post_thumbnail() ) {
                $thumb_url = get_the_post_thumbnail_url( get_the_ID(), array( 80, 80 ) );
            } else {
                $thumb_url = uwp_get_default_thumb_uri();
            }
            ?>
            <img class="uwp-profile-item-alignleft uwp-profile-item-thumb"
                 src="<?php echo esc_url_raw( $thumb_url ); ?>">
        </a>
    </div>

    <h3 class="uwp-profile-item-title">
        <a href="<?php echo esc_url_raw( get_the_permalink() ); ?>"><?php echo get_the_title(); ?></a>
    </h3>
    <time class="uwp-profile-item-time published" datetime="<?php echo get_the_time( 'c' ); ?>">
        <?php echo get_the_date(); ?>
    </time>
    <div class="uwp-profile-item-summary">
        <?php
        do_action( 'uwp_before_profile_summary', get_the_ID(), $post->post_author, $post->post_type );
        $excerpt = strip_shortcodes( wp_trim_words( get_the_excerpt(), 15, '...' ) );
        echo esc_attr( $excerpt );
        do_action( 'uwp_after_profile_summary', get_the_ID(), $post->post_author, $post->post_type );
        ?>
    </div>
</li>

Can anyone help to fetch custom post type 'properties' posts only instead of default wordpress posts? Thanks in advance.

CodePudding user response:

TO Fetch Custom Post You can use like below, update arguments as per your requirement:

$args = array(  
        'post_type' => 'services',
        'post_status' => 'publish',
        'posts_per_page' => 8, 
        'orderby’ => 'title', 
        'order’ => 'ASC', 
    );

    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 
        print the_title(); 
        the_excerpt(); 
    endwhile;

    wp_reset_postdata(); 

CodePudding user response:

You can define new WP_Query and pass 'post_type' => 'properties' as arguments. Try the below code.

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

$args = array(
    'post_type' => 'properties'
);

$properties = new WP_Query( $args );
$title = isset( $args['template_args']['title'] ) ? $args['template_args']['title'] : '';
?>
<h3><?php echo $title; ?></h3>
<div class="uwp-profile-item-block">
    <?php
    // The Loop
    if ($properties->have_posts()) {

        echo '<ul >';
        while ($properties->have_posts()) {
            $properties->the_post();
            uwp_get_template('posts-post.php', $args);
        }
        echo '</ul>';

        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
        // no posts found
        echo "<p>".sprintf( __( "No %s found.", 'userswp' ), $title )."</p>";
    }
    do_action('uwp_profile_pagination', $properties->max_num_pages);
    ?>
</div>
  • Related