WordPress.org

Ready to get started?Download WordPress

Forums

Post posts from certain month on external site (21 posts)

  1. IceFlame5
    Member
    Posted 9 months ago #

    I have been trying for about a month now to get this working but i am a complete noob with wordpress. I got wordpress to display my posts on my regular website here: cameronandrews.com/blog/index.php

    but when i try and get archives working here: cameronandrews.com/blog/archives/november2013.php it just does not want to display posts from this month. I am using this code:

    <?php
                                require('blog/wp-blog-header.php');
    							require('blog/wp-includes/general-template.php');
    							require('blog/wp-includes/link-template.php.');
                                ?>
    
                                <?php
                                $posts = get_posts('numberposts=10&order=DESC&orderby=post_date');
    
                                foreach ($posts as $post) : setup_postdata( $post ); 
    
    							i
    
                                 echo "<h2>"; the_title(); echo "</h2>";
                                 echo "<span style='font-size:11px;'>By <a href='/company/employees/cameronandrews.php'>"; the_author(); echo "</a> | Published "; the_date(); echo "</span><br /><br>";
                                 the_content(); 
    
                                endforeach;
    
                                ?>

    I want to continue to use this code. I just want it to display posts from a certain month and thats it.

  2. keesiemeijer
    moderator
    Posted 9 months ago #

    Does it show any posts?

    Try it with a date_query

    <?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $today = getdate();
    $args = array(
        'posts_per_page' => 10,
        'paged' => $paged,
        'date_query' => array(
            array(
                'year'  => $today["year"],
                'month' => $today["mon"],
            ),
        ),
    );
    $posts = get_posts($args);
    ?>

    http://codex.wordpress.org/Function_Reference/WP_Query#Date_Parameters

  3. IceFlame5
    Member
    Posted 9 months ago #

    Doesn't seem to work. I am basically building an archive system manually.

    You go to recent posts and and shows the most up to date posts at the moment that is November. But when it turns to December and i click the November archive i want it to display only posts from November. and then when i click the December archive it only shows posts from December.

    I say manually because then all i have to do is change either a number or a word in the programming for each new paged archive.

    SO at the moment i have:

    blog/index.php <- Recent Posts (Contains a sidebar where the archives are)
    blog/archives/november2013.php <- Only November Posts

    --

    when December hits i will copy the code from november2013.php and make a new page called december2013.php and just change the number or word that gives it the month thus making it work for December 2013.

  4. keesiemeijer
    moderator
    Posted 9 months ago #

    If you name your archive pages like this november-2013.php (with a dash between month and year) you can get the the year and the month from the filename and use it to query posts like this:

    <?php
    list( $year, $month ) = explode( '-', basename( __FILE__, '.php' ) );
    
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array(
    	'posts_per_page' => 10,
    	'paged' => $paged,
    	'date_query' => array(
    		array(
    			'year'  => (int) $year,
    			'month' => (int) $month,
    		),
    	),
    );
    $posts = get_posts( $args );
    ?>

    If you would use a theme WordPress would have date archives out of the box and more.

  5. IceFlame5
    Member
    Posted 9 months ago #

    So it would be this? Because this is not working. Sorry for the hassle.

    http://cameronandrews.com/blog/archives/november-2013.php

    <?php
                                require('blog/wp-blog-header.php');
    							require('blog/wp-includes/general-template.php');
                                ?>
    
                                <?php
    
    							list( $year, $month ) = explode( '-', basename( __FILE__, '.php' ) );
    
    							$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    							$args = array(
    								'posts_per_page' => 10,
    								'paged' => $paged,
    								'date_query' => array(
    									array(
    										'year'  => (int) $year,
    										'month' => (int) $month,
    									),
    								),
    							);
    							$posts = get_posts( $args );
    
                                foreach ($posts as $post) : setup_postdata( $post ); 
    
                                 echo "<h2>"; the_title(); echo "</h2>";
                                 echo "<span style='font-size:11px;'>By <a href='/company/employees/cameronandrews.php'>"; the_author(); echo "</a> | Published "; the_date(); echo "</span><br /><br>";
                                 the_content(); 
    
                                endforeach;
    
                                ?>
  6. keesiemeijer
    moderator
    Posted 9 months ago #

    No problem. I'm not that familiar with integrating WordPress with an exisiting site.
    http://codex.wordpress.org/Integrating_WordPress_with_Your_Website

    What happens when you do this (to test if any posts are shown)?

    <?php
    $args = array( 'posts_per_page' => 10 );
    $posts = get_posts( $args );
    ?>
  7. IceFlame5
    Member
    Posted 9 months ago #

  8. IceFlame5
    Member
    Posted 9 months ago #

    Never mind the basic php worked. but the code to pull the month and such didnt. Ideas as to why?

  9. keesiemeijer
    moderator
    Posted 9 months ago #

    Can you try and print the values for $year and $month:

    <?php
    list( $year, $month ) = explode( '-', basename( __FILE__, '.php' ) );
    echo $year;
    echo $month;
    ?>

    Did you put a dash in the filename november-2013.php.

  10. IceFlame5
    Member
    Posted 9 months ago #

    prints this "november2013"

    http://cameronandrews.com/blog/archives/november-2013.php

    and yes i did put the dash as you can tell by the link above.

  11. keesiemeijer
    moderator
    Posted 9 months ago #

    Ah, I see it should be:

    list( $month, $year ) = explode( '-', basename( __FILE__, '.php' ) );
  12. IceFlame5
    Member
    Posted 9 months ago #

    Is there a way i can make a "test" post for say a month from now or a month ago to make sure it doesnt pull that post. Because it seems to be working now but just in case i want to test. and i very much thank you for your help.

  13. IceFlame5
    Member
    Posted 9 months ago #

    UH OH. i figured out how to publish it so it was in say september, but it still pulls the post.

    http://cameronandrews.com/blog/archives/november-2013.php

  14. keesiemeijer
    moderator
    Posted 9 months ago #

    You can create a test post and change the date for it just above the publish button.

  15. keesiemeijer
    moderator
    Posted 9 months ago #

    Can you post all the code you have now.

  16. IceFlame5
    Member
    Posted 9 months ago #

    Yeah here you go:

    <?php
                                require('../blog/wp-blog-header.php');
    							require('../blog/wp-includes/general-template.php');
                                ?>
    
                                <?php
    							list( $month, $year ) = explode( '-', basename( __FILE__, '.php' ) );
    							$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    							$args = array(
    								'posts_per_page' => 10,
    								'paged' => $paged,
    								'date_query' => array(
    									array(
    										'year'  => (int) $year,
    										'month' => (int) $month,
    									),
    								),
    							);
    							$posts = get_posts( $args );
    
                                foreach ($posts as $post) : setup_postdata( $post ); 
    
                                 echo "<h2>"; the_title(); echo "</h2>";
                                 echo "<span style='font-size:11px;'>By <a href='/company/employees/cameronandrews.php'>"; the_author(); echo "</a> | Published "; the_date(); echo "</span><br /><br>";
                                 the_content(); 
    
                                endforeach;
    
                                ?>
  17. keesiemeijer
    moderator
    Posted 9 months ago #

    Try it with this:

    <?php
                                require('../blog/wp-blog-header.php');
                                require('../blog/wp-includes/general-template.php');
                                ?>
    
                                <?php
                                list( $month, $year ) = explode( '-', basename( __FILE__, '.php' ) );
                                $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                                $args = array(
                                    'posts_per_page' => 10,
                                    'paged'          => $paged,
                                    'year'           => $year,
                                    'monthnum'       => $month,
                                );
                                $the_query = new WP_Query( $args );
    
                                while ( $the_query->have_posts() ) : $the_query->the_post(); 
    
                                 echo "<h2>"; the_title(); echo "</h2>";
                                 echo "<span style='font-size:11px;'>By <a href='/company/employees/cameronandrews.php'>"; the_author(); echo "</a> | Published "; the_date(); echo "</span><br /><br>";
                                 the_content(); 
    
                                endwhile;
    
                                ?>
  18. IceFlame5
    Member
    Posted 9 months ago #

  19. keesiemeijer
    moderator
    Posted 9 months ago #

    Just for testing can you put this under $the_query = new WP_Query( $args );

    echo '<pre>';
    print_r($the_query);
    echo '</pre>';

    And post the what it prints here.

  20. IceFlame5
    Member
    Posted 9 months ago #

    i actually got it working! Thanks for your help. I found out the issue. its monthnum it needs to be an integer and we were giving it a string before. Thus = fail so this is what i did.

    <?php
    list( $month, $year ) = explode( '-', basename( __FILE__, '.php' ) );
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    							if($month=="january"){
    $month = "1";
    }else if($month=="february"){
    $month = "2";
    }else if($month=="march"){
    $month = "3";
    }else if($month=="april"){
    $month = "4";
    }else if($month=="may"){
    $month = "5";
    }else if($month=="june"){
    $month = "6";
    }else if($month=="july"){
    $month = "7";
    }else if($month=="august"){
    $month = "8";
    }else if($month=="september"){
    $month = "9";
    }else if($month=="october"){
    $month = "10";
    }else if($month=="november"){
    $month = "11";
    }else if($month=="december"){
    $month = "12";
    }
    
    $args = array(
    'posts_per_page' => 10,
    'paged'          => $paged,
    'year'           => $year,
    'monthnum'       => $month,
    );
    $the_query = new WP_Query( $args );
    
    while ( $the_query->have_posts() ) : $the_query->the_post(); 
    
    echo "<h2>"; the_title(); echo "</h2>";
    echo "<span style='font-size:11px;'>By <a href='/company/employees/cameronandrews.php'>"; the_author(); echo "</a> | Published "; the_date(); echo "</span><br /><br>";
    the_content(); 
    
    endwhile;
    
    ?>
  21. keesiemeijer
    moderator
    Posted 9 months ago #

    I'm glad you've got it working.

    Maybe casting it to an integer also works

    $args = array(
                                    'posts_per_page' => 10,
                                    'paged'          => $paged,
                                    'year'           => (int) $year,
                                    'monthnum'       => (int) $month,
                                );

Reply

You must log in to post.

About this Topic