我试图通过自定义字段对每个Wordpress存档进行排序,因此我在我的函数文件中添加了一个操作,但我有两个问题:
我的行动是:
add_action( 'pre_get_posts', 'my_change_sort_order');
function my_change_sort_order($query){
if(is_archive())
{
// validate
if( is_admin() )
{
return $query;
}
$query->set('meta_key', 'my_custom_field');
$query->set('orderby', 'meta_value_num');
$query->set('order', 'DESC');
return $query;
}
};
正如itzmekhokan所建议的,我也尝试过这个:
$meta_query = array(
'relation' => 'OR',
array( //check meta_key exist
'key' => 'my_custom_field',
'compare' => '!=',
'value' => ''
),
array( //if no meta_key exist
'key' => 'my_custom_field',
'compare' => 'NOT EXISTS'
)
);
$query->set( 'meta_query', $meta_query );
$query->set('meta_key', 'my_custom_field');
$query->set('orderby', 'meta_value_num');
$query->set('order', 'DESC');
然后我尝试了这个:
$meta_query = array(
'relation' => 'OR',
array( //check meta_key exist
'key' => 'my_custom_field',
'compare' => 'EXISTS'
),
array( //if no meta_key exist
'key' => 'my_custom_field',
'compare' => 'NOT EXISTS'
)
);
$query->set( 'meta_query', $meta_query );
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'my_custom_field');
$query->set('order', 'DESC');
总是没有成功。 Wordpress仅显示已设置此字段的帖子。
答案 0 :(得分:0)
public class CallReceiver extends BroadcastReceiver {
MediaRecorder recorder;
TelephonyManager telManager;
boolean recordStarted;
private Context ctx;
static boolean status = false;
String phoneNumber;
public static SharedPreferences preferences;
boolean enabled;
private static Date date;
public static Boolean isIncoming;
private static String dateStr;
@Override
public void onReceive(Context context, Intent intent) {
ctx = context;
String action = intent.getAction();
preferences = context.getSharedPreferences("Numbers", Context.MODE_PRIVATE);
enabled = preferences.getBoolean("enabled", false);
date = new Date();
dateStr = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
if (enabled) {
if (status == false) {
try {
recorder = new MediaRecorder();
} catch (Exception ex) {
ex.printStackTrace();
}
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
incomingcallrecord(action, context);
} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
incomingcallrecord(action, context);
}
}
} else {
status = false;
}
}
}
private void incomingcallrecord(String action, Context context) {
// TODO Auto-generated method stub
if (action.equals("android.intent.action.PHONE_STATE")) {
telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
private final PhoneStateListener phoneListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
Log.d("calling number", "calling number" + incomingNumber);
try {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: {
Log.e("CALL_STATE_RINGING", "CALL_STATE_RINGING");
isIncoming = true;
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
Log.e("incoming", "incoming call" + incomingNumber);
File file = createDirIfNotExists();
recorder.setOutputFile(file.getAbsolutePath());
recorder.prepare();
Thread.sleep(1000);
recorder.start();
recordStarted = true;
status = true;
Log.e("Record start", " Start");
String insertStr = "IN_" + dateStr;
ContentValues values = new ContentValues();
values.put(CallRecorderSQLite.FeedEntry.COLUMN_2_NUMBER, insertStr);
Activity_Landing.dbWritable.insert(CallRecorderSQLite.FeedEntry.TABLE_NAME, null, values);
Log.d("calling number ringing", "" + incomingNumber);
break;
}
case TelephonyManager.CALL_STATE_OFFHOOK: {
Log.e("CALL_STATE_OFFHOOK", "CALL_STATE_OFFHOOK");
isIncoming = false;
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
Log.e("outgoing", "outgoing call" + incomingNumber);
File file = createDirIfNotExists();
recorder.setOutputFile(file.getAbsolutePath());
recorder.prepare();
Thread.sleep(1000);
recorder.start();
recordStarted = true;
status = true;
Log.e("Record start", " Start");
String insertStr = "OUT_" + dateStr;
ContentValues values = new ContentValues();
values.put(CallRecorderSQLite.FeedEntry.COLUMN_2_NUMBER, insertStr);
Activity_Landing.dbWritable.insert(CallRecorderSQLite.FeedEntry.TABLE_NAME, null, values);
Log.d("calling number offhook", "" + incomingNumber);
break;
}
case TelephonyManager.CALL_STATE_IDLE: {
Log.e("CALL_STATE_IDLE", "CALL_STATE_IDLE");
if (recordStarted) {
recorder.stop();
recorder.reset();
recorder.release();
Log.e("Record stop", " stop");
recorder = null;
recordStarted = false;
}
Log.d("calling number idle", "" + incomingNumber);
break;
}
default: {
}
}
} catch (Exception ex) {
Log.e("Exception ------", "" + ex.toString());
}
}
};
public File createDirIfNotExists() {
File folder = new File(Environment.getExternalStorageDirectory() + "/PhoneCallRecording");
if (!folder.exists()) {
if (!folder.mkdirs()) {
Log.e("TravellerLog :: ", "folder is created");
}
}
File file;
if (isIncoming) {
file = new File(folder, "IN_" + dateStr + ".amr");
} else {
file = new File(folder, "OUT_" + dateStr + ".amr");
}
try {
if (!file.exists()) {
if (file.createNewFile()) {
Log.e("TravellerLog :: ", "file is created");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file;
}
}
答案 1 :(得分:0)
您需要添加空值,其中您有数组,其中包含“不存在”字样。比较键值:
array( //if no meta_key exist
'key' => 'my_custom_field',
'value'=>'',
'compare' => 'NOT EXISTS'
)
另外,尝试更改您拥有的阵列' EXISTS'进入这个:
array( //check meta_key exist
'key' => 'my_custom_field',
'value'=> 'somerandomvalue123456', //value that obviously won't be in records at all
'compare' => '<>'
),
这样,查询的构造将考虑“不存在”&#39;部分,因为值键不会丢失,它会搜索具有此类元键的其他帖子,但不会在查询中设置一些随机值。 这有一个特定的错误: https://core.trac.wordpress.org/ticket/23268#comment:22并且版本4.1中存在,不确定是否仍然存在,因为票证中没有注释指出该问题实际上已解决。