tax query shortcode

时间:2016-02-28 作者:Daniel Florido

事实证明,很难用税务查询配置我的快捷码。当我省略分类法/术语的参数时,我什么也没有得到。

这将按预期返回1篇文章

[loop post_type="website" taxonomy="industry" terms="beauty" posts="3"][/loop]
但是,当为分类法省略参数时,当我想取回所有网站帖子时,它不会返回帖子。

[loop post_type="website" posts="-1" ][/loop]
My shortcode函数

function loop_recent_posts_shortcode($atts) {

  $tax_queries = array(
      array (
    \'taxonomy\' => $atts[\'taxonomy\'],
    \'terms\' => array($atts[\'terms\']),
    \'field\' => \'slug\'
      )
    );

  $atts = shortcode_atts(
    array(
    \'posts\' => 6,
    \'post_type\' => \'\',
    \'taxonomy\' => \'\',
    \'terms\' => \'\',
    \'meta_key\'  => \'\',
    \'meta_value\' => 0, 
    ), $atts);
   $return_string = \'\';
   $return_string .= \'<ul class = "work_loop">\';
   $args = array(
     \'post_type\' => $atts[\'post_type\'], 
     \'tax_query\' => $tax_queries,
     \'orderby\' => \'date\', 
     \'order\' => \'DESC\', 
     \'meta_key\' => $atts[\'meta_key\'], 
     \'meta_value\' => $atts[\'meta_value\'], 
     \'showposts\' => $atts[\'posts\'],
      );
   $sc_query = new WP_Query( $args);
   var_dump($sc_query); 
   if ($sc_query->have_posts()) :
      while ($sc_query->have_posts()) : $sc_query->the_post();
   //POST WITH THUMBNAIL
  if (has_post_thumbnail($id)) {
    $return_string .= \'<li><a href="\' . get_permalink();
    $return_string .= \'"><span class = "icon more_icon"></span><img src = "\' . ml_get_thumbnail(null, \'3_col_folio\') . \'" alt = "websites"><span class = "triangle_up"></span><h2>\'.get_the_title().\'</h2></a></li>\';
  } else {
    //POST WITHOUT THUMBNAIL
     $return_string .= \'<li><a href="\' . get_permalink();
     $return_string .= \'"><h2>\'.get_the_title().\'</h2></a></li>\';
  }
  endwhile;
  endif;

   $return_string .= \'</ul>\';

   wp_reset_query();
   return $return_string;

}
这里是sc\\U查询的转储

object(WP_Query)#925 (51) { ["query"]=> array(7) { ["post_type"]=> string(7) "website" ["tax_query"]=> array(1) { [0]=> array(3) { ["taxonomy"]=> NULL ["terms"]=> array(1) { [0]=> NULL } ["field"]=> string(4) "slug" } } ["orderby"]=> string(4) "date" ["order"]=> string(4) "DESC" ["meta_key"]=> string(0) "" ["meta_value"]=> int(0) ["showposts"]=> string(2) "-1" } ["query_vars"]=> array(66) { ["post_type"]=> string(7) "website" ["tax_query"]=> array(1) { [0]=> array(3) { ["taxonomy"]=> NULL ["terms"]=> array(1) { [0]=> NULL } ["field"]=> string(4) "slug" } } ["orderby"]=> string(4) "date" ["order"]=> string(4) "DESC" ["meta_key"]=> string(0) "" ["meta_value"]=> int(0) ["showposts"]=> int(-1) ["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) ["comments_popup"]=> string(0) "" ["preview"]=> string(0) "" ["s"]=> string(0) "" ["sentence"]=> string(0) "" ["title"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> 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) { } ["ignore_sticky_posts"]=> bool(false) ["suppress_filters"]=> bool(false) ["cache_results"]=> bool(true) ["update_post_term_cache"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["posts_per_page"]=> int(-1) ["nopaging"]=> bool(true) ["comments_per_page"]=> string(2) "50" ["no_found_rows"]=> bool(false) } ["tax_query"]=> object(WP_Tax_Query)#927 (6) { ["queries"]=> array(1) { [0]=> array(5) { ["taxonomy"]=> NULL ["terms"]=> array(1) { [0]=> NULL } ["field"]=> string(4) "slug" ["operator"]=> string(2) "IN" ["include_children"]=> bool(true) } } ["relation"]=> string(3) "AND" ["table_aliases":protected]=> array(0) { } ["queried_terms"]=> array(0) { } ["primary_table"]=> string(8) "wp_posts" ["primary_id_column"]=> string(2) "ID" } ["meta_query"]=> object(WP_Meta_Query)#922 (9) { ["queries"]=> array(2) { [0]=> array(1) { ["value"]=> int(0) } ["relation"]=> string(2) "OR" } ["relation"]=> string(3) "AND" ["meta_table"]=> string(11) "wp_postmeta" ["meta_id_column"]=> string(7) "post_id" ["primary_table"]=> string(8) "wp_posts" ["primary_id_column"]=> string(2) "ID" ["table_aliases":protected]=> array(1) { [0]=> string(11) "wp_postmeta" } ["clauses":protected]=> array(1) { ["wp_postmeta"]=> array(4) { ["value"]=> int(0) ["compare"]=> string(1) "=" ["alias"]=> string(11) "wp_postmeta" ["cast"]=> string(4) "CHAR" } } ["has_or_relation":protected]=> bool(false) } ["date_query"]=> bool(false) ["request"]=> string(386) "SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( 0 = 1 ) AND ( CAST(wp_postmeta.meta_value AS CHAR) = \'0\' ) AND wp_posts.post_type = \'website\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'acf-disabled\' OR wp_posts.post_status = \'private\') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC " ["posts"]=> array(0) { } ["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(true) ["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(true) ["is_search"]=> bool(false) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(false) ["is_404"]=> bool(false) ["is_embed"]=> bool(false) ["is_comments_popup"]=> 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(true) ["query_vars_hash":"WP_Query":private]=> string(32) "e809d2ab096418440d3b2ab2568e9629" ["query_vars_changed":"WP_Query":private]=> bool(false) ["thumbnails_cached"]=> bool(false) ["updated_term_meta_cache"]=> bool(false) ["updated_comment_meta_cache"]=> 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" } }

1 个回复
最合适的回答,由SO网友:jgraup 整理而成

除了在设置了所有值后仅添加税务查询外,您只需确保设置了所有默认属性。

<?php

// [loop post_type="website" taxonomy="industry" terms="beauty" posts="3"][/loop]
// [loop post_type="website" posts="-1" ][/loop]

function loop_recent_posts_shortcode( $atts ) {

    $a = shortcode_atts(
        array (
            \'posts\'      => 6,
            \'post_type\'  => \'\',
            \'taxonomy\'   => \'\',
            \'terms\'      => \'\',
            \'meta_key\'   => \'\',
            \'meta_value\' => 0,
        ), $atts );

    $args = array (
        \'post_type\' => array ( $a[ \'post_type\' ] ),
        \'orderby\'   => \'date\',
        \'order\'     => \'DESC\',
        \'showposts\' => $a[ \'posts\' ],
        \'fields\'    => \'ids\',
    );

    // only add if we have both keys in the original values

    if ( isset( $atts[ \'meta_key\' ], $atts[ \'meta_value\' ] ) ) {
        $args[ \'meta_key\' ]   = $a[ \'meta_key\' ];
        $args[ \'meta_value\' ] = $a[ \'meta_value\' ];
    }

    // only add if we have both keys in the original values

    if ( isset( $atts[ \'taxonomy\' ], $atts[ \'terms\' ] ) ) {
        $tax_queries         = array (
            array (
                \'field\'    => \'slug\',
                \'taxonomy\' => $a[ \'taxonomy\' ],
                \'terms\'    => $a[ \'terms\' ],
            ),
        );
        $args[ \'tax_query\' ] = $tax_queries;
    }

    // run the query

    $q = new WP_Query( $args );

    $content = \'\';
    $content .= \'<ul class = "work_loop">\' . PHP_EOL;

    if ( $q->have_posts() ) :
        while( $q->have_posts() ) : $q->the_post();

            // THUMBNAIL
            if ( has_post_thumbnail( get_the_ID() ) ) {
                $thumnail_src = function_exists( \'ml_get_thumbnail\' ) ? ml_get_thumbnail( null, \'3_col_folio\' ) : get_the_post_thumbnail_url( null, \'3_col_folio\' );
                $content .= \'<li><a href="\' . get_the_permalink();
                $content .= \'"><span class="icon more_icon"></span><img src="\' . $thumnail_src
                            . \'" alt="websites"><span class="triangle_up"></span><h2>\'
                            . get_the_title() . \'</h2></a></li>\' . PHP_EOL;
            }
            else {
                // NO THUMBNAIL
                $content .= \'<li><a href="\' . get_the_permalink();
                $content .= \'"><h2>\' . get_the_title() . \'</h2></a></li>\' . PHP_EOL;
            }

        endwhile; // the_post()
    else:
        //... no posts

    endif; // have_posts()
    wp_reset_postdata();

    $content .= \'</ul>\';

    return $content;
}

add_shortcode( \'loop\', \'loop_recent_posts_shortcode\' );

相关推荐

SHORTCODE_ATTS()中的$ATTS参数是什么?

这个WordPress developers reference page for shortcode_atts() 国家:$atts(array)(必选)用户在shortcode标记中定义的属性。但我不理解这个定义。例如,在WP Frontend Profile 插件:$atts = shortcode_atts( [ \'role\' => \'\', ], $atts ); 据我所知,shortcode\