• Resolved noobhome

    (@noobhome)


    Hi, I’m using this function that modifies the number of posts showed only in homepage, without affecting other categories.

    /**
     * Fixing the Number of Post
     * Used in Homepage
     */
    function simplecatch_home_custom_query( $query ) {
    
    	if (  is_home() )
    		$query->query_vars['posts_per_page'] = 5; // Change 5 to the number of posts you would like to show
    
    	return $query; // Return our modified query variables
    }
    add_filter( 'pre_get_posts', 'simplecatch_home_custom_query' ); // Hook our custom function onto the request filter

    This works perfect, but my problem is that a text widget in my home page is also using the posts_per_page argument, so that full widget gets multiplied by 5 (the new post_per_page that I am forcing with this function).

    This is my text widget code (it’s a box widget that works as a “RANDOM POST” button):

    <?php
    $args=array(
      'orderby' => 'rand',
      'cat' => '14',
      'post_type' => 'page',
      'post_status' => 'publish',
      'posts_per_page' => 1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo '';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <div style="width: 272px; padding: 5px; border: 4px solid gray; margin: 0; text-align: center; background-color: rgb(183, 222, 253); "><h4><font-size=6><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">Can?ó aleatòria</a></font><h4></div>
    
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    So, what I think I need is something that makes that this widget on my homepage ignores this function, so it preserves his ‘posts_per_page’ => 1.

    I would really apreciate your help, thank you very much in advanced.

    PS: sorry for my English.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Try this in your ‘pre_get_posts’ conditional:
    if ( is_home() && is_main_query() )

    That should work, but it might not. You may be able to identify something between the two queries that distinguishes one from the other. For example, if your post per page option is normally 10, you can check for the corresponding query var value in ‘pre_get_posts’ before you change it. The main query would have the value 10 and the widget query would have the value 1.

    Thread Starter noobhome

    (@noobhome)

    Thank you for answering.
    I tried that (if ( is_home() && is_main_query() )) but didn’t work. The widget is still getting multiplied.

    I didn’t get what was your second advice. Could you explain that? In all my other pages, the post_per_page is set to 999, I did it from the menu (Settings > Reading).

    Thread Starter noobhome

    (@noobhome)

    This is my posts_per_page.php :

    <?php
    /**
     * Function used to alter the ammount of posts per page
     *
     * @package WordPress
     * @subpackage GoPress WPExplorer Theme
     * @since GoPress 1.0
     */
    
    // Alter portfolio taxonomy posts per page
    if ( !function_exists('wpex_pre_get_posts') ) {
    
    	function wpex_pre_get_posts($query) {
    
    		// Portfolio taxonomy
    		if ( is_tax( 'portfolio_category') ) {
    			$posts_per_page = get_theme_mod('wpex_portfolio_posts_per_page', '12');
    			$query->set( 'posts_per_page', $posts_per_page );
    		}
    
    	}
    
    }
    add_filter( 'pre_get_posts', 'wpex_pre_get_posts' );

    Moderator bcworkz

    (@bcworkz)

    Before getting into my second idea, try this variant of the first, it might make a difference:
    if ( $query->is_home() && $query->is_main_query() )

    The posts per page value was just an example of what could be different between the two queries. It also doesn’t matter if the value is 10, 999, 25, or anything besides 1. As long as they are different. All WP_Queries go through the ‘pre_get_posts’ action, so this is the best place to manage how many posts to return.

    If your widget query is the only query that comes through where posts_per_page is 1, adding this to the conditional will prevent the value from being altered as it is for other queries:
    1 != $query->get('posts_per_page')

    You should consider consolidating all of your pre_get_posts hooks into one for easier management of the various conditions. You could have switch/case and/or if/elseif/else structures to arrive at the correct value for any condition.

    You are not limited to the is_*() booleans or posts_per_page to determine the proper conditions. Any value in the $query object could be used. It’s a sizable object, you should be able to find some difference. It would be helpful to dump out the entire object for each query for analysis, but IIRC, simply inserting a var_dump() in your action callback doesn’t end up being displayed. Try it anyway, I could be wrong.

    You may need to get more creative to see the query values. I use the output buffer to capture var_dumps, then output the result in the footer. I have a friend who emails himself the output. You could write it to a log file. There are other possibilities as well.

    I hope this makes more sense. If not, try to explain what part is not making sense and I’ll try to explain in different terms.

    Thread Starter noobhome

    (@noobhome)

    This variant worked perfectly! (if ( $query->is_home() && $query->is_main_query() ))

    Thank you very much, it was really useful.

    Moderator bcworkz

    (@bcworkz)

    Great! You’re most welcome. Too bad I didn’t get it right the first time around ??

    FWIW, my second idea is an important concept to keep in mind for the future. That is assuming you will be continuing to hack queries at some point and that you are able to make sense of my description. Even if not, maybe it’ll help someone.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to order a widget to ignore a function of functions.php’ is closed to new replies.