9 posts
  • Has been part of the Envato Community for over 3 years
  • Located in Australia
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
smartsites1 says

Hello Everyone! I am building a new WordPress theme and I am using the WordPress ‘Option Tree’ plugin for the theme options area. I have created a setting in the theme options Labelled ‘Category Box 1’ with the id of ‘category_box_one’ and option type of ‘Category Select’ so with that I am trying to set up my theme so that whatever category the user selects for ‘Category Box 1’ in the theme options area is the category that will be used for a wp query in my index file and will output posts from the chosen category…..I have tried and tried and googled a soltuion to this but I am still not sure exactly how to set the wp query so that it can do this…..does anyone have any tips on what to do to accomplish this?

1477 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+3 more
OriginalEXE says

I don’t know how OptionTree saves data, but it would go something like this:

$catfromoption = array( 1, 2, 3, 4 ); // replace this with your get option function, where you retrieve array of categories that was saved via options tree

function theme_custom_query_filter( $query ) {
    if ( is_home() || is_front_page() ) { // main page is in use, execute our filter
        $query->set( 'category__in', $catfromoption );
    }
}
add_action( 'pre_get_posts', 'theme_custom_query_filter' );
9 posts
  • Has been part of the Envato Community for over 3 years
  • Located in Australia
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
smartsites1 says

Thanks for the reply OriginalEXE, I am going to try it out now :)

9 posts
  • Has been part of the Envato Community for over 3 years
  • Located in Australia
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
smartsites1 says

So if my option was something like this:

if ( function_exists( 'ot_get_option' ) ) {
$category_box_one = ot_get_option( 'category_box_one' );
}

How would I then integrate it into your code above?

1477 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+3 more
OriginalEXE says
It would go something like this:
if ( function_exists( 'ot_get_option' ) ) {

$category_box_one = ( array ) ot_get_option( 'category_box_one' );

function theme_custom_query_filter( $query ) {
    if ( is_home() || is_front_page() ) { // main page is in use, execute our filter
        $query->set( 'category__in', $category_box_one );
    }
}
add_action( 'pre_get_posts', 'theme_custom_query_filter' );

}
9 posts
  • Has been part of the Envato Community for over 3 years
  • Located in Australia
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
smartsites1 says

Ok, I tried that and nothing is appearing…but both statements are true ‘the option does exist’ and ‘it is the frontpage’. Where should I put that code for it to work? Do I need to add another function with it?

Thanks for the great help so far :grin:

1477 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+3 more
OriginalEXE says

Can you please put this in your header.php and let me know of the output (it will be visible on your page).

<?php $category_box_one = ( array ) ot_get_option( 'category_box_one' ); var_dump( $category_box_one ); ?>
9 posts
  • Has been part of the Envato Community for over 3 years
  • Located in Australia
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
smartsites1 says
The output that is shown is
array(1) { [0]=> string(1) "5"
1477 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+3 more
OriginalEXE says
Ok, try this code then: (in your functions.php of the theme)
if ( function_exists( 'ot_get_option' ) ) {

$category_box_one = ( array ) ot_get_option( 'category_box_one' );

$category_box_one = array_map( 'intval', $category_box_one );

function theme_custom_query_filter( $query ) {
    if ( is_home() || is_front_page() ) { // main page is in use, execute our filter
        $query->set( 'category__in', $category_box_one );
    }
}
add_action( 'pre_get_posts', 'theme_custom_query_filter' );

}
9 posts
  • Has been part of the Envato Community for over 3 years
  • Located in Australia
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
smartsites1 says

hmm, I tried that and nothing is still appearing…

Do multiple WP queries on one page conflict?

by
by
by
by
by
by