Created
October 5, 2020 20:17
-
-
Save slavapas/353f1a9da76d51736f69668b7bfb457c to your computer and use it in GitHub Desktop.
Define and post Custom Post Type #Wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Register Custom Post Type in functions.php | |
add_action( 'pre_get_posts', 'add_my_post_types_to_query' ); | |
function add_my_post_types_to_query( $query ) { | |
if ( is_home() && $query->is_main_query() ) | |
$query->set( 'post_type', array( 'post', 'movies' ) ); | |
return $query; | |
} | |
//Querying Custom Post Types | |
<?php | |
$args = array( | |
'post_type' => 'movies', | |
'posts_per_page' => -1 | |
); | |
$the_query = new WP_Query( $args ); | |
?> | |
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> | |
<h2><?php the_title(); ?></h2> | |
<div class="entry-content"> | |
<?php the_content(); ?> | |
</div> | |
<?php else: ?> | |
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p> | |
<?php endwhile; ?> | |
<!-- reset global post variable. After this point, we are back to the Main Query object --> | |
<?php wp_reset_postdata(); ?> | |
<?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment