How to merge query in wordpress with different arguments - merge

$all = array(
'post_type' => array("pwpd_poll","pwpd_infog"),
'posts_per_page' => wp_is_mobile() ? 3 : 7,
'orderby' => 'post_date',
'order' => 'DESC'
);
$news = array(
'post_type' =>array('pwpd_news'),
'meta_query' => $meta_query_par,
'date_query'=>array(
array(
'year' => $today['year'],
'month' => $today['mon'],
'day' => $today['mday'],
)
)
);
the problem is the $news output is always in the end of the loop the "orderby" doesnt seem to be working and also the "posts_per_page" is not exact in the output.

Related

How to use where/whereDate in mongodb aggregate functions using Jessengers package in Laravel?

How to use where/whereDate in aggregate mongodb query ?
I am using aggregate query in laravel and jessenger-mongodb to group by fields.
The problem here is, I need to add a condition to fetch grouped data of a particular date i.e created_at.
Following code worked perfectly if I do not use pass $date i.e $date = null:
public static function getUsersByTypeLogin($date = null)
{
$q = self::raw()->aggregate(
[
array(
'$match' => array(
'type' => self::LOGIN,
)
),
array(
'$group' => array(
'_id' => array(
'lat' => '$latitude',
'long' => '$longitude',
'country' => '$country',
'state' => '$state'
),
'count' => array('$sum' => 1)
)
)
]
);
if(true == $date){
$q = $q->whereDate('created_at','=',$date);
}
return $q;
}
but If I pass $date it returns an error, it says:
message: "Call to undefined method MongoDB\Driver\Cursor::whereDate()"
$date is a Carbon object e.g:
Carbon #1549843200 {#791
date: 2019-02-11 00:00:00.0 +00:00
}
Please help me in this regards.
Try this.
$q = self::raw(function($collection) use($date) {
return $collection->aggregate([
array(
'$match' => array(
'type' => self::LOGIN,
'created_date' => $date,
)
),
array(
'$group' => array(
'_id' => array(
'lat' => '$latitude',
'long' => '$longitude',
'country' => '$country',
'state' => '$state'
),
'count' => array('$sum' => 1)
)
)
]);
});

Sort with results matching condition on top

I have written mongodb aggregation query in php like below lines of code.
$orrollno= array('$or' => array(array("student.roll_no" => new MongoRegex("/$arg/i"))));
$orlastname= array('$or' => array(array("student.last_name" => new MongoRegex("/$arg/i"))));
$oremail= array('$or' => array(array("student.email" => new MongoRegex("/$arg/i"))));
$orguardian= array('$or' => array(array("student.guardian_name" => new MongoRegex("/$arg/i"))));
$orphone= array('$or' => array(array("student.phone1" => new MongoRegex("/$arg/i"))));
$orfullname= array('$or' => array(array("fullname" => new MongoRegex("/$arg/i"))));
$orfirstmiddle= array('$or' => array(array("firstmiddle" => new MongoRegex("/$arg/i"))));
$orfirstlast= array('$or' => array(array("firstlast" => new MongoRegex("/$arg/i"))));
$query = array( '$or' => array($orrollno,$orlastname,$oremail,$orguardian,$orphone,$orfullname,$orfirstmiddle,$orfirstlast));
$outputTotalResults= $this->db->studentTbl->aggregate(
array(
array(
'$project' => array(
'fullname' => array('$concat' => array('$first_name', ' ', '$middle_name', ' ', '$last_name')),
'firstmiddle' => array('$concat' => array('$first_name', ' ', '$middle_name')),
'firstlast' => array('$concat' => array('$first_name', ' ', '$last_name')),
'student' => '$$ROOT'
)
),
array(
'$match' => $query
),
)
);
I am trying to sort the results which comes from $match => $query.
For e.g $arg contains "William David" then results should first contain the records with names Willian David and then the rest of the results.
Any help shall be greatly appreciated!!!
Based on ur suggestion I have now tried the below
$outputTotalResults= $this->db->studentTbl->aggregate(
array(
array(
'$project' => array(
'fullname' => array('$concat' => array('$first_name', ' ', '$middle_name', ' ', '$last_name')),
'firstmiddle' => array('$concat' => array('$first_name', ' ', '$middle_name')),
'firstlast' => array('$concat' => array('$first_name', ' ', '$last_name')),
'student' => '$$ROOT',
'weight' => array(
'$cond' => array(
array(
'$or' => array(
array('$eq' => array('$fullname' => $arg )),
array('$eq' => array('$firstmiddle' => $arg)),
array('$eq' => array('$firstlast' => $arg)),
)
),
10,
0
)
),
array(
'$sort' => array( 'weight'=> -1 )
),
array(
'$match' => $query
),
)
)
)
);
What you want to achieve here is a "weighted sort", where you essentially want to calculate a field based on conditions and then apply a $sort pipeline stage to that result.
The general case is to apply $cond with a logical condition and either return a value or not, possibly for more than one condition in a cascading way.
Ideally with MongoDB 3.4 and above, use $addFields:
array(
array(
'$addFields' => array(
'weight' => array(
'$cond => array(
array(
'$and' => array(
array( '$eq' => array( '$first_name', 'Willam' ) )
array( '$eq' => array( '$last_name', 'David' ) )
)
),
10,
0
)
)
)
),
array(
'$sort' => array( 'weight'=> -1 )
)
)
Or in prior versions where you cannot simply "append" a new field to the existing document structure you use $project, either specifying each field you want or altering the structure returning under one property via $$ROOT:
array(
array(
'$project' => array(
'first_name' => 1,
'last_name' => 1,
'weight' => array(
'$cond => array(
array(
'$and' => array(
array( '$eq' => array( '$first_name', 'Willam' ) )
array( '$eq' => array( '$last_name', 'David' ) )
)
),
10,
0
)
)
)
),
array(
'$sort' => array( 'weight'=> -1 )
)
)
So in that simple case, whenever the "both" conditions (via $and) are met, the weight property is assigned a value of 10, otherwise it gets a 0. The subsequent sort on the weight property is in "descending" order, so all the 10 values where the condition matched will be on "top", whilst all other results will come after all the matches.
This is how you would structure for your exact implementation. First you $match your query conditions as this reduces the overall documents to process and this is the only time an aggregation pipeline can actually use an index.
Then you $project the field with the comparisons for whether the match phrase was in the preferred fields, and finally $sort on that calculated field.
array(
array( '$match' => $query ),
array(
'$addFields' => array(
'weight' => array(
'$cond => array(
array(
'$or' => array(
array(
'$eq' => array(
array('$concat' => array('$first_name', ' ', '$middle_name', ' ', '$last_name')),
$arg
)
),
array(
'$eq' => array(
array('$concat' => array('$first_name', ' ', '$middle_name')),
$arg
)
),
array(
'$eq' => array(
array('$concat' => array('$first_name', ' ', '$last_name')),
$arg
)
)
)
),
10,
0
)
)
)
),
array(
'$sort' => array( 'weight'=> -1 )
)
)
So always $match first or otherwise use a pipeline stage that is going to use an index and "optimize" your result. Then manipulate and remember that you cannot use calculated fields for comparison in a "single" $project phase. If you really need it then you either duplicate the calculations or do the calculations in one stage and then compare on the values in the next stage.
Honestly though, once you go to these lengths you are basically reproducing what a text search, in which you can:
Spread the index across all the fields you want to search in. This eliminates the massive $or condition into a simple query operation.
Specify a weighting on the particular fields where the match would be more important.
The only case where a "text search" would not be the best solution is if the fields you want "more weight on" change on a regular basis. Since text indexes have "set" values for the weighting and you can have only one per collection, then you cannot easily change the combination of fields to assign more weight to. With the aggregation process shown, its fairly trivial to change around the fields and assignment of weight.

RealURL error: wrong "L" parameter value

RealURL gives this error.
My site has only one language, and I have not added any website language in root page.
I tried with config.linkVars = L and config.linkVars = L(0-5) in the setup with no effect.
RealURL Config is auto-generated:
<?php
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl'] = array(
'_DEFAULT' => array(
'init' => array(
'appendMissingSlash' => 'ifNotFile,redirect',
'emptyUrlReturnValue' => '/'
),
'pagePath' => array(
'rootpage_id' => '1'
),
'fileName' => array(
'defaultToHTMLsuffixOnPrev' => 0,
'acceptHTMLsuffix' => 1,
'index' => array(
'print' => array(
'keyValues' => array(
'type' => 98
)
)
)
),
'postVarSets' => array(
'_DEFAULT' => array(
'news' => array(
0 => array(
'GETvar' => 'tx_news_pi1[news]',
'lookUpTable' => array(
'table' => 'tx_news_domain_model_news',
'id_field' => 'uid',
'alias_field' => 'title',
'useUniqueCache' => 1,
'useUniqueCache_conf' => array(
'strtolower' => 1,
'spaceCharacter' => '-'
)
)
)
)
)
)
)
);
I think thats a RealURL 2.1.7 problem.
I downgrade to RealURL 2.1.5 and then it works.
EDIT:
Now i add page.theme.language.languageValue = 0 to my template constants
See => https://github.com/dmitryd/typo3-realurl/issues/407
A quick fix for 2.1.7 can be a strict assignment of the configured "uid" integer values
linkVars = L(0-1)
… did the job for me. Instead of L(0-3)

WP_query category__in array only pulls from first category id

I am trying to get ALL post id's for posts in these categories (relation OR): 10,11,12,13,14 with certain extra attributes. My problem is with the categories however.
My args array for my wp_query is as follows:
$args = array(
'orderby' =>'ID',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'category__in' => array('10','11','12','13','14'),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'joke_type',
'value' => $type,
'type' => 'CHAR',
'compare' => '='
),
array(
'key' => 'joke_rating',
'value' => 3,
'type' => 'SIGNED',
'compare' => '>='
)
),
'fields' => 'ids'
);
This only gets me posts from category 10 (or whichever ID I place first in the array). I also tried: 'category__in' => '10,11,12,13,14' and 'category' => '10,11,12,13,14' and 'cat' => '10,11,12,13,14' All act the same.
Any idea why this could be happening?
Category IDs inside your category__in array should be integers rather than strings.
Change:
'category__in' => array('10','11','12','13','14'),
To:
'category__in' => array( 10, 11, 12, 13, 14 ),

Cakephp aggregation query (MongoDB)

Using ichikaway-cakephp I am trying to convert following query (running fine in php) to cakephp
In cakephp it returns empty array
Core PHP
$out = $collection->aggregate(array(
array('$unwind' => '$as'),
array(
'$group' => array(
'_id' => array('as'=>'$as'),
'count' => array('$sum' => 1)
)
)
));
Cakephp
$conditions=array('aggregate'=>array(
array('$unwind' => '$as'),
array(
'$group' => array(
'_id' => array('as'=>'$as'),
'count' => array('$sum' => 1)
)
)
));
$results = $this->Post->find('all',array('conditions'=>$conditions));
I am unable to find aggrgation framework function in test cases
So far only this commit talks about aggregation.
$params = array(
array('$unwind' => '$as'),
array(
'$group' => array(
'_id' => array('as'=>'$as'),
'count' => array('$sum' => 1)
)
)
));
$mongo = $this->Post->getDataSource();
$mongoCollectionObject = $mongo->getMongoCollection($this->Post);
$results = $mongoCollectionObject->aggregate($params);