get custom post archieve by month - wordpress

I want to make a custom post archieve template where I will display only the custom post by month..
I added this in archieve.php
$args = array( 'post_type' => 'news_letter', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
/** Include the post format-specific template for the content. If you want to
* this in a child theme then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'newletter', get_post_format() );
where news_letter is my custom post...by this I am getting all post without any sorting of month, means I am getting all month post in December link.
to make archieve link I used this:
<li><?php wp_get_archives(array('type' => 'monthly','order'=>'ASC')); ?></li>
any help would be appreciate..
Thanks for your time and sharing knowledge... :)

I think you have not used 'has_archive' parameter when creating custom post type.
Please refer below code for creating custom post type.
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'news_letter',
array(
'labels' => array(
'name' => __( 'News Letter' ),
'singular_name' => __( 'News Letter' )
),
'public' => true,
'has_archive' => true,
)
);
}
then in archive-{posttype}.php file paste below code:
<?php
get_header();
if(have_posts()) : while(have_posts()) : the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile; endif;
get_footer();
?>
Let me know if you have any query.
Thanks.

Related

new "custom post template" cant be load in new post template list

here in WordPress i'm going to add a Custom Post Type , named : MultiMedia
i have created a Custom Post Type by CPTUI plugin and also have paste the Code provided by CPTUI from "Get Code" Menu to the end of Funtion.php :
function cptui_register_my_cpts_multimedia() {
/**
* Post Type: MultiMedia.
*/
$labels = array(
"name" => __( 'MultiMedia', '' ),
"singular_name" => __( 'MultiMedia', '' ),
.
.
.
"attributes" => __( 'MultiMedia Attributes', '' ),
"parent_item_colon" => __( 'Parent MultiMedia:', '' ),
);
$args = array(
"label" => __( 'MultiMedia', '' ),
"labels" => $labels,
.
.
.
"supports" => array( "title", "editor", "thumbnail", "custom-fields" ),
"taxonomies" => array( "category", "post_tag" ),
);
register_post_type( "multimedia", $args );
}
add_action( 'init', 'cptui_register_my_cpts_multimedia' );
then i created a multimedia.php in the theme Folder with:
<?php /* Template Name Posts: MultiMedia */ ?>
<?php get_header(); ?>
<div class="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
MultiMedia post type is working!
<?php endwhile; ?>
<?php endif; ?>
</div> <!-- end #main -->
<?php get_footer(); ?>
the problem is multimedia is not in the DropDown List of templates in NewPost sidebar as were expected
is this the correct way?
help me to fix this
-thanks
Edit your code from <?php /* Template Name Posts: MultiMedia */ ?> to <?php /* Template name: MultiMedia */ ?> should do the trick for you.For more information about creating a own template, go to this site or here

user comments display on wordpress page

looking to achieve something similar to this page where user can enter a thought into a form field and on submission this then posts straight onto the page.
http://hcma.ca/
Would using wordpress comments be best for this? Or some way of sending the form submission to populate a repeater advanced custom field on the page. Can anyone advise how best to achieve this?
Also wondering about spam. The site above has no captcha or similar (as far as I can tell). What's the deal with this?
Thanks!
If you don't want to use a paid plugin, you can create a new Custom Post Type and then display the results on the page you want. You should follow this tutorial here
In short:
Create a new post type:
// Our custom post type function
function create_posttype() {
register_post_type( 'movies',
// CPT Options
array(
'labels' => array(
'name' => __( 'Movies' ),
'singular_name' => __( 'Movie' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'movies'),
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
Display the results in a page:
<?php
$args = array( 'post_type' => 'movies', 'posts_per_page' => 10 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
You can use then this free plugin to add Google reCaptcha to avoid spam

Pagination shows empty pages for date based archive.php loop

I'm using wordpress and have a custom archive page for a custom post type.
The custom loop gets the logged in users registration date and only shows posts that were published on or after they registered. Works great.
However, the pagination is still using the main query so if there are 4 posts in total set to 2 posts per page the pagination always shows there are two pages even if only one post is displayed due to the logged in users registered date.
Can anyone help me modify what I have so the pagination only shows for results in more than 2 posts for that users query? I've been trying for hours now using various changes I've found on the web...
<?php if ( have_posts() ): ?>
<?php
# Get the current user's info
$user_info = get_userdata(get_current_user_id());
# Use date_parse to cast your date to an array
$regdate = date_parse($user_info->user_registered);
# Set your arguments for WP Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'inner',
'posts_per_page' => '2',
'posts_per_archive_page' => '2',
'paged' => $paged,
'date_query' => array(
array(
'after' => array(
# Setting date to array above allows to call specific values within that date
'year' => $regdate['year'],
'month' => $regdate['month'],
'day' => $regdate['day'],
),
# Include posts from the day the user registered
'inclusive' => true,
),
),
# Display all posts on a single page.
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile; ?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else: ?>
Nada
<?php endif; ?>
Working with Custom Archive and Pagination
#Scott Eldo Your approach with create custom query will not change main query on your custom archive. FYI and you are correct, pagination only work for main query.
In your case, the recommended approach, I will use filter pre_get_posts to work with custom archive and pagination. Please take a look my answer here how to modify main query on post type archive page, you can figure it out with your query parameters.
BUT if you intent to create query direct into your template ( even it is not change main query ), you need to match your custom query with $GLOBALS['wp_query'] that use in pagination and don't forget to use wp_reset_query() ( MUST ). Take a look my approach here related with your code:
<?php if ( have_posts() ): ?>
<?php
# Get the current user's info
$user_info = get_userdata(get_current_user_id());
# Use date_parse to cast your date to an array
$regdate = date_parse($user_info->user_registered);
# Set your arguments for WP Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'inner',
'posts_per_page' => '2',
'posts_per_archive_page' => '2',
'paged' => $paged,
'date_query' => array(
array(
'after' => array(
# Setting date to array above allows to call specific values within that date
'year' => $regdate['year'],
'month' => $regdate['month'],
'day' => $regdate['day'],
),
# Include posts from the day the user registered
'inclusive' => true,
),
),
# Display all posts on a single page.
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile; ?>
<?php
/**
* Fix Pagination in custom page/archive
* Set global wp_query the same as our custom query
*
* Use function the_posts_pagination( $args ); for pagination will work too
* see https://developer.wordpress.org/reference/functions/get_the_posts_pagination/#source-code
*/
$GLOBALS['wp_query'] = $my_query;
?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else: ?>
Nada
<?php endif; ?>
<?php wp_reset_query(); ?> // MUST use to reset global query and post data
Another approach and still NOT recommended is use query_post for your custom query. More reading about it you can learn more in here.

Wordpress, I don't want to display the featured image when I query custom post types

How can I stop the featured image from displaying in my custom post types queries?
my code when quering
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array(
'post_type' => array(
'weddings',
'e_shoots_and_couples',
'kids_familiy',
'portraits',
'other_shoots'
),
'posts_per_page' => 10,
'paged' => $paged
) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<h2 class="entry-title">', '</h2>' ); ?><div id="slider-date"><?php echo get_the_date(); ?></div>
<div class="entry-content">
<?php the_content();
/*$content = get_the_excerpt();
$postOutput = preg_replace('/<img[^>]+./','', $content);
echo $postOutput;*/
?>
</div>
<?php endwhile; ?>
Thanks
Please take a look as the link add theme support--Post Thumbnails
You can set thumbnail support for specific post types or all post type by using the add_theme_support function in functions.php file of your theme.
This feature enables post-thumbnail support for a Theme. This feature became available with Version 2.9. Note that you can optionally pass a second argument with an array of the post types for which you want to enable this feature.
add_theme_support('post-thumbnails');
To enable only for Posts:
add_theme_support( 'post-thumbnails', array( 'post' ) );
Or only Pages:
add_theme_support( 'post-thumbnails', array( 'page' ) );
Enable for Posts and "movie" post type but not for Pages.
add_theme_support( 'post-thumbnails', array( 'post', 'movie' ) );
This feature must be called before the init hook is fired. That means it needs to be placed directly into functions.php or within a function attached to the after_setup_theme hook:
For custom post types, you can also add post thumbnails using the register_post_type function as well.

Custom Post Type Small Help?

I have add custom post type field name 'Movies'. Now I have done these things and its working great, but the problem is, (i.e When I'm click on any movie, its showing me only one movie post, (i.e I'm click on avatar movie its showing me avatar movie post, but when I'm click on stargate movie its showing me avatar movie post. Please help its a big issue) anyone who can help me to make this code exactly which I want.
in my functions.php I have add this code:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'Movies',
array(
'labels' => array(
'name' => __( 'movie' ),
'singular_name' => __( 'movie' )
),
'public' => true,
'has_archive' => true,
)
);
}
Then in my template file add this where I want to show post:
<?php
$args = array( 'post_type' => 'movies', 'posts_per_page' => 1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1>
<a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a>
</h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
I'm new for this thing, so please explain me as much as you can, where I paste it or what I do?
I can't see how you structured your links to those movie-posts, but you should give them a variable, so the page where the movie-post shows up knows what to show!
e.g $moviename
and in your template you modify the $args array to:
$args = array( 'post_type' => 'movies', 'posts_per_page' => 1, 'name' => $_GET['movie'] );
Should work, at least that explains why it always displays the same movie-post:
Your query hast no information what movie to display, at the moment it just takes the movie-posts table and displays the first one - because of the posts_per_page limit to 1.
Hope that makes sense...

Resources