Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to output a topic list using the main loop


  • mackymickman
    Participant

    @mackymickman

    I am branching pages in this code:

    // BuddyPress
    $arr = explode( '/', $_SERVER['REQUEST_URI'] );
    if ( isset($arr[1]) && $arr[1] === 'members' ) :
    
    	// Pages that only the himself can access
    	if( $arr[3] === 'messages' || $arr[3] === 'settings' || $arr[3] === 'notifications' ) :
    		get_template_part( 'template-parts/page', 'bp-only' );
    	
    	// Pages that other users can access
    	else :
    		get_template_part( 'template-parts/page', 'bp-public' );
    	
    	endif;
    
    endif;	

    However, on the above code, $wp_query does not work in “page-bp-public.php”.
    For example, I use $wp_query as below to output topic list, but topic title is not output.

    function my_pre_get_posts( $query ) {
    	$query->set('post_type', 'topic');
    }
    add_action('pre_get_posts','my_pre_get_posts');
    <?php global $wp_query; ?>
    <?php if ($wp_query->have_posts()): ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    	<?php echo get_the_title(); ?>
    <?php endwhile; ?>
    <?php else: ?>
    	<p>nothing</p>
    <?php endif; ?>

    How can I use $wp_query on “page-bp-public.php”?

    ・・・

    By the way, I var_dump to $wp_query and it will be as follows:

    <pre><?php var_dump($wp_query->query_vars);?></pre>

    <pre>array(65) {
      ["page"]=>
      int(0)
      ["pagename"]=>
      string(6) "forums"
      ["error"]=>
      string(0) ""
      ["m"]=>
      string(0) ""
      ["p"]=>
      int(0)
      ["post_parent"]=>
      string(0) ""
      ["subpost"]=>
      string(0) ""
      ["subpost_id"]=>
      string(0) ""
      ["attachment"]=>
      string(0) ""
      ["attachment_id"]=>
      int(0)
      ["name"]=>
      string(6) "forums"
      ["static"]=>
      string(0) ""
      ["page_id"]=>
      int(0)
      ["second"]=>
      string(0) ""
      ["minute"]=>
      string(0) ""
      ["hour"]=>
      string(0) ""
      ["day"]=>
      int(0)
      ["monthnum"]=>
      int(0)
      ["year"]=>
      int(0)
      ["w"]=>
      int(0)
      ["category_name"]=>
      string(0) ""
      ["tag"]=>
      string(0) ""
      ["cat"]=>
      string(0) ""
      ["tag_id"]=>
      string(0) ""
      ["author"]=>
      string(0) ""
      ["author_name"]=>
      string(0) ""
      ["feed"]=>
      string(0) ""
      ["tb"]=>
      string(0) ""
      ["paged"]=>
      int(0)
      ["meta_key"]=>
      string(0) ""
      ["meta_value"]=>
      string(0) ""
      ["preview"]=>
      string(0) ""
      ["s"]=>
      string(0) ""
      ["sentence"]=>
      string(0) ""
      ["title"]=>
      string(0) ""
      ["fields"]=>
      string(0) ""
      ["menu_order"]=>
      string(0) ""
      ["embed"]=>
      string(0) ""
      ["category__in"]=>
      array(0) {
      }
      ["category__not_in"]=>
      array(0) {
      }
      ["category__and"]=>
      array(0) {
      }
      ["post__in"]=>
      array(0) {
      }
      ["post__not_in"]=>
      array(0) {
      }
      ["post_name__in"]=>
      array(0) {
      }
      ["tag__in"]=>
      array(0) {
      }
      ["tag__not_in"]=>
      array(0) {
      }
      ["tag__and"]=>
      array(0) {
      }
      ["tag_slug__in"]=>
      array(0) {
      }
      ["tag_slug__and"]=>
      array(0) {
      }
      ["post_parent__in"]=>
      array(0) {
      }
      ["post_parent__not_in"]=>
      array(0) {
      }
      ["author__in"]=>
      array(0) {
      }
      ["author__not_in"]=>
      array(0) {
      }
      ["ignore_sticky_posts"]=>
      bool(false)
      ["suppress_filters"]=>
      bool(false)
      ["cache_results"]=>
      bool(true)
      ["update_post_term_cache"]=>
      bool(true)
      ["lazy_load_term_meta"]=>
      bool(true)
      ["update_post_meta_cache"]=>
      bool(true)
      ["post_type"]=>
      string(0) ""
      ["posts_per_page"]=>
      int(1)
      ["nopaging"]=>
      bool(false)
      ["comments_per_page"]=>
      string(2) "50"
      ["no_found_rows"]=>
      bool(false)
      ["order"]=>
      string(4) "DESC"
    }
    </pre>
Viewing 3 replies - 1 through 3 (of 3 total)

  • shanebp
    Moderator

    @shanebp

    page-bp-public.php is not in the BuddyPress plugin.

    $query->set('post_type', 'topic');
    ‘topic’ post types are created and handled by bbPress.


    mackymickman
    Participant

    @mackymickman

    I have already made “page-bp-public.php”, and BBPress is also installed.
    In addition, the above $wp_query works with “index.php”.
    I wonder what’s fault this is??


    shanebp
    Moderator

    @shanebp

    Please ask questions about bbPress on their support forums.

    It’s not easy to figure out what you are doing – or more importantly why.

    I suggest you not use pre_get_posts.
    Instead write your own WP_Query on your custom page.

    This thread is now closed due to being off-topic for these forums.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to output a topic list using the main loop’ is closed to new replies.
Skip to toolbar