Jump to content

Recommended Posts

This must be easy enough, but I'm a noob so go easy on me, please :-)

 

I have two pieces of code and they're working well on their own. All I need is to put these two conditions into one query.

 

The first one:

<?php
function childtheme_cat_limited_blog( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'events_categories', 'mycategory1' );
    }
}
add_action( 'pre_get_posts', 'childtheme_cat_limited_blog' );
?>

And the second:

<?php
function childtheme_cat_limited_blog( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'category_name', 'mycategory2' );
    }
}
add_action( 'pre_get_posts', 'childtheme_cat_limited_blog' );
?>

I'm sure it's a piece a cake for you guys, but I can't figure it out for the life of me, so any help would be greatly appreciated!

 

Thank you!

 

(My goal is here to display posts from two categories on a homepage of a Wordpress site, but to complicate things the two categories are from two different post types.)

Link to comment
https://forums.phpfreaks.com/topic/292455-merging-two-queries/
Share on other sites

Not a wordpress user but does the following work?

function childtheme_cat_limited_blog( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'events_categories', 'mycategory1' );
        $query->set( 'category_name', 'mycategory2' );
    }
}
add_action( 'pre_get_posts', 'childtheme_cat_limited_blog' );
Link to comment
https://forums.phpfreaks.com/topic/292455-merging-two-queries/#findComment-1496587
Share on other sites

  • 4 weeks later...

Try this:

function childtheme_cat_limited_blog( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        return $query;
    }
    $query->set( 'events_categories', 'mycategory1' );
    $query->set( 'category_name', 'mycategory2' );
    return $query;
}
add_filter( 'pre_get_posts', 'childtheme_cat_limited_blog' );

I'm not sure if events categories is a custom taxonomy or not - if it is, I'm not sure the above will work, but there are ways around that too so post back here if it doesn't work.

Link to comment
https://forums.phpfreaks.com/topic/292455-merging-two-queries/#findComment-1499128
Share on other sites

I am taking a form that has been created and now trying to incorporate it into WP. The form is filled out multiple times per session. The form when filled out will generate an ID that is testking.net  used to track the form submissions until completed. The problem that I'm running into is that the code is using a URL structure that breaks on the install.

Link to comment
https://forums.phpfreaks.com/topic/292455-merging-two-queries/#findComment-1499263
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.