检查与当前帖子类型不同的自定义字段值,然后执行某些操作

时间:2017-12-06 作者:730wavy

我有两种帖子类型,一种用于我的帖子,另一种用作提交表单。

当一个用户在一个单独的帖子页面上发布类型a时,我会尝试进行一些检查,然后显示内容-

1) 我正在检查用户是否已登录

2) 如果他在B类帖子中发布了任何帖子(提交的表格)

3) 如果是,那么作者发布的帖子类型B中的自定义字段(1)是否为空

4) 如果不为空,该值是否与当前正在查看的帖子类型A中任何帖子的自定义字段(2)的值匹配

这就是我目前所拥有的---

<?php 
$user = wp_get_current_user();
$user_id = get_current_user_id();
$balance = mycred_get_users_balance( $user_id, \'piq_credits\' );
if( (is_user_logged_in()) && (piq_user_has_posts($user->ID)) && ($balance != \'0\') ) { ?>
piq\\u user\\u has\\u posts函数为--

function piq_user_has_posts($user_id) {
  $result = new WP_Query(array(
    \'author\'=>$user_id,
    \'post_type\'=>\'post_type_b\',
    \'post_status\'=>\'publish\',
    \'posts_per_page\'=>1,
  ));
  return (count($result->posts)!=0);
}
我现在需要的是能够检查-if $customfield1 = $customfield2 then do something

同样,每个自定义字段都来自不同的帖子类型,我在一个帖子页面上为其中一个帖子类型执行此操作。

到目前为止,我尝试了以下方法--

$users_query = new WP_Query( array(
    \'post_type\' => \'piq_request\',
    \'meta_key\' => \'piq_link\',
    \'meta_value\' => $piq_link,
) );

//echo $piq_link;
echo $this_property_id;

if($piq_link == $this_property_id) {

2 个回复
最合适的回答,由SO网友:730wavy 整理而成

我的工作几乎完美无瑕。但我需要进一步测试我的else语句。

我的职能中有这一点。php--

function piq_user_has_posts($user_id) {
  $result = new WP_Query(array(
    \'author\'=>$user_id,
    \'post_type\'=>\'post_type_b\',
    \'post_status\'=>\'publish\',
    \'posts_per_page\'=>1,
  ));
  return (count($result->posts)!=0);
}
这是我的单个贴子页面上的贴子类型a--

<?php

// GET USER INFO - SOME FIELDS ARE PROBABLY NOT NEEDED 
$user = wp_get_current_user();
$user_id = get_current_user_id();

//$balance = mycred_get_users_balance( $user_id, \'piq_credits\' );

// IN FUNCTIONS FILE HAS CODE FOR PIQ_USER_HAS_POSTS TO SEE IF THE USER HAS POSTS IN SPECIFIC CUSTOM POST TYPE. THEN I CHECK IF LOGGED IN
if( (piq_user_has_posts($user->ID)) && (is_user_logged_in()) ) { 

// ONCE THAT CHECKS OUT I RUN A QUERY FOR ALL THE POSTS IN POST TYPE B
$args = array(
  \'numberposts\'=> -1,           
  \'post_type\'=> \'post_type_b\',
  \'meta_key\' => \'piq_link\',
  \'meta_value\' => $this_property_id, // THE POST ID FOR THE POST WE ARE CURRENTLY ON IN POST TYPE A
  \'author\' => $user_id,
);

$my_query = new WP_Query($args);
if( $my_query->have_posts() ) :
  while ($my_query->have_posts()) : $my_query->the_post();

// IF USER POSTS MATCH THEN I GET THE VALUES NEEDED TO RUN MY OTHER CHECKS
$user_id = get_current_user_id();
$piq_author = get_the_author_meta( \'ID\' );
$piq_link = get_post_meta(get_the_id(),\'piq_link\',true);

// I CHECK IF ALL THE VALUES MATCH UP
if (($this_property_id == $piq_link) && ($user_id == $piq_author)) { ?>

<!-- STUFF I WANT TO SHOW IF EVERYTHING CHECKS OUT OK -->


<?php } // CLOSE IT OUT
endwhile;
endif; wp_reset_query(); // END THE LOOP ?>

<!-- IF USER HAS NO POSTS AND IS NOT LOGGED IN -->

<?php } else { ?>
NOPE
<?php } ?>

SO网友:David Sword

如果我理解正确,我相信这会帮助你得到你想要的东西。我无法验证它是否有效,但阅读它应该可以提供一些指导:

function my_plugin_verify() {

    // --------------------------------------------------------------------------
    // 1) I\'m checking if the user is logged in
    if (!is_user_logged_in())
        return false; // NOT VERIFIED

    // --------------------------------------------------------------------------
    // 2) if he has published any posts (submitted forms) in post_typeB
    $user_id = get_current_user_id();

    // get all posts_typeB by $user_id
    $users_posts_typeB = get_posts(array(
                    \'author\' => $user_id,
                    \'post_type\' => \'post_typeB\',
                    \'post_status\' => \'publish\',
                    \'posts_per_page\' => -1,
                ));

    // if no posts, NOT VERIFIED
    if (count($users_posts) < 1)
        return false; 

    // --------------------------------------------------------------------------
    // 3) is custom_field_1 in $users_posts_typeB in post type B empty or not
    $custom_field_1_values = array();

    // cycle through post_typeB posts by $user_id
    foreach ($users_posts_typeB as $apost) {

        // see if custom_field_1 is not empty in all them
        $custom_field_1 = get_post_meta($apost->ID,\'custom_field_1\',true);

        // if custom_field_1 empty, user is NOT VERIFIED
        if (empty($custom_field_1) || empty($custom_field_1))
            return false; 

        // if custom_field_1 has value, record it for comparison next
        else
            $custom_field_1_values[]  = $custom_field_1;
    }

    // --------------------------------------------------------------------------
    // 4) does the value match the value of custom field (2) of any posts in post type A that is currently being viewed

    // we\'ll presume no matches
    $matchFound = false;

    // we\'ll query all post_typeA, looking for a custom_field_2 with the value of all above custom_field_1 value\'s
    foreach ($custom_field_1_values as $custom_field_1_value) {
        $matchingCustomValue = get_posts(array(
            \'post_type\' => \'post_typeA\',
            \'post_status\' => \'publish\',
            \'posts_per_page\' => -1,
            \'meta_key\' => \'custom_field_2\',
            \'meta_value\' => $custom_field_1_value,
        ));

        // if there\'s a match, we\'ll note it
        if (count($matchingCustomValue) > 0)
            $matchFound = true;
    }

    // if there\'s no match, user is NOT VERIFIED
    if (!$matchFound)
        return false;

    // --------------------------------------------------------------------------
    // 5) Is the mycred (plugin) balance is empty or not
    $balance = mycred_get_users_balance( $user_id, \'piq_credits\' );
    if ($balance == \'0\')
        return false

    // --------------------------------------------------------------------------
    // they got this far, they\'re verified
    return true;
}
你只需要讨论函数

if ( my_plugin_verify() )
    // user meets criteria
    // display content
)
我不确定(4)中的“当前正在查看”是什么意思,以及您将如何监视它。

我不确定post\\u typeB是否会返回多个post,或者custom\\u field\\u 1是否可以为多个用户存在。我写的是倍数。

如果您的查询不必实时进行,也不需要不断更新,那么您需要transients 保存验证状态,而不是每次查询。

结束

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在