Support » Developing with WordPress » Custom Post Types not showing in any widgets

  • Hello All,
    I’m having issues getting custom post types to show up anywhere but the archives. I’m sure it is something to do with pre_get_posts but I’m not sure what to do. I have my current query below to get it working on the archive pages. My custom post types are ‘artists’ and ‘releases’. I’ve also tried adding $query->set(‘post_type’,array(‘artists’,’release’,’post’,’page’);
    but then the archive pages don’t show correctly.

    function set_category_query($query) {
      if(!is_admin() && (is_category() || is_tag()) && $query->is_main_query()) {
        $post_type = get_query_var('post_type');
        $query->set('orderby','title');
        $query->set('order','ASC');
        $query->set('posts_per_page',50);
        if ( is_category('groups') ||  is_category('bands') ||  is_category('dub_group') ||  is_category('vocal_duos') ||  is_category('vocal_groups') ||  is_category('individuals') ||  is_category('dub_engineers') ||  is_category('mixers') ||  is_category('musicians') || is_category('nonmusicians') || is_category('producers') || is_category('solo_artists')) {    
    			$query->set('post_type','artists');
        } elseif ( is_category('releases') ||  is_category('masters') || is_category('versions') || is_category('albums') || is_category('eps') || is_category('singles') ) {
          $query->set('post_type','releases');
        }
    		if ($query->is_home()) {
          $query->set('post_type',array('artists','releases','post') );
    		}
    	}
    	return $query;
    }
    
    add_filter('pre_get_posts', 'set_category_query');

    The page I need help with: [log in to see the link]

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

    (@t-p)

    For pro or commercial product support please contact the author directly on their site.

    https://themepacific.com/bfastmag/

    Commercial products are not supported in these forums.

    Thread Starter cjjr82

    (@cjjr82)

    Hello t-p
    Thanks for the reply, but I am not asking for support on a pro or commercial product. As said, I am asking for help with WordPress itself and specifically pre_get_posts. This issue is not theme specific. As can be seen on the main page of my site now, https://jamaicansounds.com/ the widget on the right is the default recent posts widget and none of the custom post types are showing in it. None of the custom post types show anywhere but the archive pages.
    Thanks

    • This reply was modified 5 years, 2 months ago by cjjr82.
    • This reply was modified 5 years, 2 months ago by cjjr82.
    Moderator bcworkz

    (@bcworkz)

    You’re on the right path with setting “post_type” to an array of post types. What you must do to avoid influencing queries you don’t want to influence is properly apply conditional arguments before setting the query_var. It can be tricky at times to properly identify the correct conditions. If you var_dump() the passed query object, you ought to be able to identify some combination of factors that uniquely identify the query you want to change.

    Instead of or in addition to checking for the correct factors, you can sometimes manage when your add_action() call executes as a way to control when the post_type query var should be set. Since you are trying to target widget queries, you could call add_action() directly on the widget area template (often sidebar.php). If necessary, callbacks can remove themselves from action stacks to avoid impacting other queries that may occur after widgets.

    Or write your own widget to query the correct post types, because Recent Posts is just for Posts.

    Thread Starter cjjr82

    (@cjjr82)

    Hey bcworkz,
    Thanks for the constructive response and push in the var_dump direction. The add_action part kind of lost me, I’m still learning about WordPress.
    Now though, I seem to have hit a new issue. I did a var_dump right before a few widgets just to see and on the Ultimate Post widget it is showing the post_type array has the custom post types in it, but they still aren’t showing up. I have added the query if anyone would be able to lend any other thoughts on this issue. In the first line, it shows the post_type array, which include artists and releases, neither of which are showing up.

    Ultimate Posts
    object(WP_Query)#1712 (46) { ["query"]=> array(2) { [0]=> string(9) "post_type" [1]=> array(4) { [0]=> string(7) "artists" [1]=> string(8) "releases" [2]=> string(4) "post" [3]=> string(5) "songs" } } ["query_vars"]=> array(54) { [0]=> string(9) "post_type" [1]=> array(4) { [0]=> string(7) "artists" [1]=> string(8) "releases" [2]=> string(4) "post" [3]=> string(5) "songs" } ["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(0) "" ["static"]=> string(0) "" ["pagename"]=> 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) { } } ["tax_query"]=> object(WP_Tax_Query)#1708 (6) { ["queries"]=> array(0) { } ["relation"]=> string(3) "AND" ["table_aliases":protected]=> array(0) { } ["queried_terms"]=> array(0) { } ["primary_table"]=> NULL ["primary_id_column"]=> NULL } ["meta_query"]=> bool(false) ["date_query"]=> bool(false) ["post_count"]=> int(0) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["found_posts"]=> int(0) ["max_num_pages"]=> int(0) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(false) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(false) ["is_tag"]=> bool(false) ["is_tax"]=> bool(false) ["is_search"]=> bool(false) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(true) ["is_404"]=> bool(false) ["is_embed"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash":"WP_Query":private]=> string(32) "38caf350c7f4881a60186a0033a32dbf" ["query_vars_changed":"WP_Query":private]=> bool(false) ["thumbnails_cached"]=> bool(false) ["stopwords":"WP_Query":private]=> NULL ["compat_fields":"WP_Query":private]=> array(2) { [0]=> string(15) "query_vars_hash" [1]=> string(18) "query_vars_changed" } ["compat_methods":"WP_Query":private]=> array(2) { [0]=> string(16) "init_query_flags" [1]=> string(15) "parse_tax_query" } }
    Moderator bcworkz

    (@bcworkz)

    It’s really hard to see why a query fails from an pre_get_posts object dump. It’s a lot more informative to examine the actual SQL query that WP builds from the query vars. To see that, hook the “posts_request” filter and echo out the the passed string. Add the hook as close to the related query usage, then have your callback remove itself so you don’t get a bunch of unrelated SQL strings.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom Post Types not showing in any widgets’ is closed to new replies.