WordPress.org

Ready to get started?Download WordPress

Forums

Display post link for 1 hour (7 posts)

  1. Steekvlam
    Member
    Posted 1 year ago #

    Hi,

    Im trying to make a widget to show the upcomming posts by day.
    When a post is posted there will be a live feed on youtube, how can I display that link for one hour on the widget?

    Greetings

    Steekvlam

  2. chilinut
    Member
    Posted 1 year ago #

    http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

    Below is a modified version of the codex example for getting posts within a date range that should retrieve posts from the last hour

    // Create a new filtering function that will add our where clause to the query
    function filter_where( $where = '' ) {
    	// posts in the last HOUR
    	$where .= " AND post_date > '" . date('Y-m-d-H-i-s', strtotime('-1 hour')) . "'";
    	return $where;
    }
    
    add_filter( 'posts_where', 'filter_where' );
    $query = new WP_Query( $query_string );
    remove_filter( 'posts_where', 'filter_where' );
  3. Steekvlam
    Member
    Posted 1 year ago #

    Looks good!
    But what will the query be for in the widget?
    The query must be different then the other posts, only the widget must show the live post.

    Thanks!

  4. Steekvlam
    Member
    Posted 1 year ago #

    Can someone tell me how to use it in the theme?

  5. chilinut
    Member
    Posted 1 year ago #

    if your category you are getting posts from is called youtube-feed then your query string might look like this

    $query = new WP_Query( 'category_name=youtube-feed' );

    note that category_name requires a Category SLUG as input

    if you want to do it by category name instead of slug, then it would be

    $my_cat_ID=get_cat_id('Youtube');
    $queryString="cat=".$my_cat_ID;

    so based on the filter code from my last post, you would simply insert the query string code directly before the filter code. This is code that will work by placing it in the php file of the page you are modifying. You can refer to the codex to learn how to display the posts returned by the query. However, if you are actually making a widget, something that will live in widgets.php, then it is more complicated and you can refer to the codex entry on widgets
    http://codex.wordpress.org/Widgets_API

    and beyond that, google is your friend, which returned this page here

    http://www.makeuseof.com/tag/how-to-create-wordpress-widgets/

  6. Steekvlam
    Member
    Posted 1 year ago #

    Hi chilinut,

    The filter wont change the other queries?
    Cause there are 4 other pages with normal queries.

    Greetings

  7. chilinut
    Member
    Posted 1 year ago #

    It should only affect the ones you want. Notice the last 3 lines:

    add_filter( 'posts_where', 'filter_where' );
    $query = new WP_Query( $query_string );
    remove_filter( 'posts_where', 'filter_where' );

    the filter is added, applied to the query you want, then immediately removed!

Topic Closed

This topic has been closed to new replies.

About this Topic