• Hi,

    I have a code in functions.php to show only aside, quote, and image post formats on home page. This one:

    function mostrar_capa( $query ) {
    	if ( $query->is_home() && $query->is_main_query() ) {
    		$query->set( 'post_format', array('post-format-aside', 'post-format-image', 'post-format-quote' ) );
    	}
    }
    add_action( 'pre_get_posts', 'mostrar_capa' );

    It works flawlessly! However, there are a few other type of content that I would like to show on home page alongside those post formats. For instance, posts from a different post type called “podcast”. I tried this modification:

    function mostrar_capa( $query ) {
    	if ( $query->is_home() && $query->is_main_query() ) {
    		$query->set( 'post_format', array('post-format-aside', 'post-format-image', 'post-format-quote' ) );
    		$query->set( 'post_type', 'podcast' );
    	}
    }
    add_action( 'pre_get_posts', 'mostrar_capa' );

    It worked, but not as I intended. This code sums both lines (AND), so it only shows posts of “podcast” type that are at the same time an aside, quote, or image. I want those lines to be kind of independent (OR), so if only one criteria/line is true, the post shows up.

    (I checked if $query->set( 'post_type', 'podcast' ); is valid running it by itself. It works.)

    Any idea how can I do this? My coding level is pretty basic, so I’d appreciate detailed replies.

    Thanks!

    • This topic was modified 3 years, 10 months ago by Rodrigo. Reason: Typo
Viewing 2 replies - 1 through 2 (of 2 total)
  • Well, you have to think about that: how would the parser know that you want podcast posts OR regular posts of a certain format? It can only fetch posts matching all of the options you specify. You would have to do two separate queries and merge them, I guess, but I’m not able to provide you any code. There should be something similar somewhere on the internet you could adapt.

    Moderator bcworkz

    (@bcworkz)

    In some cases you can filter the SQL and swap the correct AND with OR, but the SQL in this case isn’t conducive to that trick. An alternative to making two queries would be to go ahead and get all formats and suppress specific format output in the PHP loop.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Multiple criteria on $query->set with results based on OR instead of AND’ is closed to new replies.