Search
 
SCRIPT & CODE EXAMPLE
 

PHP

wp php related post by category

function example_cats_related_post() {

    $post_id = get_the_ID();
    $cat_ids = array();
    $categories = get_the_category( $post_id );

    if(!empty($categories) && !is_wp_error($categories)):
        foreach ($categories as $category):
            array_push($cat_ids, $category->term_id);
        endforeach;
    endif;

    $current_post_type = get_post_type($post_id);

    $query_args = array( 
        'category__in'   => $cat_ids,
        'post_type'      => $current_post_type,
        'post__not_in'    => array($post_id),
        'posts_per_page'  => '3',
     );

    $related_cats_post = new WP_Query( $query_args );

    if($related_cats_post->have_posts()):
         while($related_cats_post->have_posts()): $related_cats_post->the_post(); ?>
            <ul>
                <li>
                    <a href="<?php the_permalink(); ?>">
                        <?php the_title(); ?>
                    </a>
                    <?php the_content(); ?>
                </li>
            </ul>
        <?php endwhile;

        // Restore original Post Data
        wp_reset_postdata();
     endif;

}
Comment

PREVIOUS NEXT
Code Example
Php :: php remove span tags from string 
Php :: remove all html codes using php 
Php :: how to remove token while logout using laravel 8 
Php :: attach multiple files in laravel mailable 
Php :: php ip address of visitor 
Php :: date_default_timezone_set for india in php 
Php :: laravel foreign key 
Php :: change php version ubuntu 
Php :: php carbon get timestamp 
Php :: laravel assign active based on route name 
Php :: total no of occurances in string php 
Php :: wordpress disable errors 
Php :: carbon parse subday 
Php :: How to show php text 
Php :: php get start and end date of month and year 
Php :: php set selected option 
Php :: Connecting to the database using mysqli 
Php :: php to string 
Php :: xml to object php 
Php :: php subtract mins to datetime 
Php :: get product price wordpress 
Php :: how to add property to the request object in laravel 
Php :: laravel eloquent multiple primary key 
Php :: php pop off the first character of string 
Php :: laravel fillable 
Php :: codeigniter get parameter from url 
Php :: php convert date from dd/mm/yyyy to yyyy-mm-dd 
Php :: php url parse 
Php :: use wordpress functions in external php file 
Php :: get user symfony 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =