Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Function works everywhere except home page

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #31395
    rmlumley
    Member

    I have a function that pulls in a custom post_type (events) and shows the two upcoming events. It works fine when I pull in the function in a post or page’s sidebar. When I use it on the home page (the one place I need it work at) – it doesn’t work correctly.

    
    $todaysDate = date('Y/m/d');
    $event_widget_query = new WP_Query('post_type=events&posts_per_page=2&post_status=publish&meta_key=event_date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC');
    ?>

    If I simplify it on the homepage:

    
    $todaysDate = date('Y/m/d');
    $event_widget_query = new WP_Query('post_type=events&posts_per_page=2');
    ?>

    It just pulls in the two most recent posts – not the two most recent events. What can I do so that this function works on the home page? Is it a multiple loop issue? I’ve tried rewind_posts and wp_reset_query.

    I’ve also posted more information on stack overflow.

    #63687
    rmlumley
    Member

    This has been an issue I’ve been struggling with for weeks, so anyone who can take a look at this would be great. I’m simply at a loss of what else to try at this point.

    #63640
    clokey2k
    Participant

    I thought I had found a solution for you here: http://justintadlock.com/archives/2010/02/02/showing-custom-post-types-on-your-home-blog-page

    BUT I was wrong!! I cannot fathom why the home page would alter the query code. I see you have tried to do the same thing with ‘query_posts()’ (practically the same as ‘WP_query’). Try to echo out the query string, before and after you create your query – just to make sure that your query is not being modified. (‘echo $query_string’)

    #63643
    rmlumley
    Member

    Thanks for looking into it. There are two sidebars (sidebar.php and sidebar-home.php). For testing purposes, I have them both calling the function. It only works on posts and pages. If I change it so that home.php calls in the normal sidebar (instead of sidebar-home) – it doesn’t work. If I change single.php so it calls in sidebar-home (instead of the normal sidebar) – it doesn’t work. So, my thought is something must be affecting it inside of home.php. I tried setting it up so the very first thing that home.php does is call in the sidebar. It still doesn’t work.

    Home.php is simply running a loop.

    I might be misunderstanding you, but I tried putting in before and after the wp_query and nothing gets outputted.

    #63629
    clokey2k
    Participant

    oh… the $query_string trick was from Chris’s ‘The WordPress Loop’.

    I’m none the wiser as to why it’s broken…

    #63630
    rmlumley
    Member

    Thanks for looking into it. I’m glad I’m not crazy in thinking everything looks right. There is some sort of conflict with home.php and I just don’t know what it is.

    #63608
    rmlumley
    Member

    I actually got it to work using get_posts.

    #63588
    rmlumley
    Member

    Code that solved the problem posted below:

    
    function eventsWidget() {?>

    Featured Events



      global $post;
      $todaysDate = date('Y/m/d');
      $event_args = array('post_type' => 'events',
      'numberposts' => 2,
      'meta_key' => 'event_date',
      'post_status' => 'publish',
      'meta_compare' => '>=',
      'meta_value' => $todaysDate,
      'orderby' => 'meta_value',
      'order'=> 'ASC');
      $event_widget_query = get_posts( $event_args );
      foreach ($event_widget_query as $post) : setup_postdata($event_widget_query); ?>
      ID, 'event_date', true);
      $new_event_date = date("F j",strtotime($event_date)); ?>
    • "> -  »



    more events »




    #118056
    UverHlfod
    Member

    Thanks for the bonus! I just submitted a review for Tradewinds Cruise Club – BVI’s. It’s an article that I wrote and will be published in December 2012.
    senior care

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘Back End’ is closed to new topics and replies.