Wordpress: Display posts by custom select query - wordpress

I need to get posts by custom category id, sort by custom field value and with another custom field if this field exists. I think I need to use custom selection query. Look at my query: The problem is that this query returns the same post 5 times... In admin panel I made 20 posts per page. Maybe someone have their own solution ? Thanks.
$wp_query->request = "SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id)
WHERE $wpdb->terms.slug = 'categoryname'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
ORDER BY $wpdb->posts.post_date DESC";
$pageposts = $wpdb->get_results($wp_query->request, OBJECT);

$customSelect = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 10,
'taxonomy' => 'category',
'order' => 'DESC',
'meta_key' => 'your custom field',
'orderby' => 'meta_value'
This is probably way off what your trying to achieve, the only bit I can't get my head round is how to test if meta key exists using a variable or something.
Anyways good luck

Why you use "$wp_query->request", this variable is for retrieving last query result, not to set new one...
just try
$my_custom_sql = "SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id)
WHERE $wpdb->terms.slug = 'categoryname'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
ORDER BY $wpdb->posts.post_date DESC";
$pageposts = $wpdb->get_results($my_custom_sql, OBJECT);

Try this-
get_results(
"
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = 3
ORDER BY post_date ASC
"
); ?>

Related

WP_Query get other table column values

I am trying to fetch another table column by WP_Query but it does not work.
Note: I can easily do this by $wpdb->get_results( 'custom_sql' ) but I want to do it by WP_Query
My code
function join_my_query( $join, $wp_query ) {
global $wpdb;
if ( $wp_query->get( 'custom_logic' ) === true ) {
$join .= " LEFT JOIN $wpdb->postmeta as pm ON $wpdb->posts.ID = pm.post_id ";
}
return $join;
}
add_filter( 'posts_join', 'join_my_query', 10, 2 );
$query = new WP_Query(
array(
'custom_logic' => true,
'post_type' => 'post',
)
);
It's giving me SQL like below, which does not select any column from the joined table!
Generated SQL
SELECT
SQL_CALC_FOUND_ROWS wp_posts.ID
FROM
wp_posts
LEFT JOIN wp_postmeta as pm ON wp_posts.ID = pm.post_id
WHERE
1 = 1
AND (
(
wp_posts.post_type = 'post'
AND (
wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'future'
OR wp_posts.post_status = 'draft'
OR wp_posts.post_status = 'pending'
)
)
)
ORDER BY
wp_posts.post_date DESC
LIMIT
0, 10
It does not select any column from my joined table. I have use var_dump($query->posts[0]) which return a WP_Post object and it has no column from my joined table. How I can get columns from my joined table with WP_Query like below
My Goal
$post = $query->posts[0];
$post->meta_key; // from my joined table, not work
$post->pm->meta_key; // pm is alias used in join, still not work
According to your expected result, the posts_join filter is not required. You can do this with the posts_clauses filter easily!
add_filter( 'posts_clauses', 'modify_post_clauses_callback' , 10, 2 );
function modify_post_clauses_callback( $clauses, $wp_query ) {
if ( $wp_query->get( 'custom_logic' ) === true ) {
global $wpdb;
$clauses['fields'] = $clauses['fields'] . ', pm.meta_key';
$clauses['join'] = "LEFT JOIN {$wpdb->postmeta} as pm ON {$wpdb->posts}.ID = pm.post_id";
}
return $clauses;
}
$query = new WP_Query(
array(
'custom_logic' => true,
'post_type' => 'post',
)
);
$post = $query->posts[0];
$post->meta_key;

Get ACF repeater field values from all posts, sorted by a sub-field

I have an ACF repeater field (publications) with 2 sub-fields.
title and year.
I need to select from the database all titles from all posts matching a condition (let’s say all titles which include ‘search-term’) but I need the result sorted by the year (the 2nd sub-field).
This is the query I use to fetch the titles.
function get_search_results(): array
{
global $wpdb;
$sql = "SELECT pm.meta_value title, pm.post_id post
FROM {$wpdb->posts} p
JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
WHERE p.post_status = 'publish'
AND pm.meta_key LIKE 'publication_%_title'
AND pm.meta_value LIKE '%search-term%';";
return $wpdb->get_results($sql, ARRAY_A);
}
How can I sort the results by the year?
This is the solution I came up with.
Only 2 SQL queries.
function get_search_results(): array
{
global $wpdb;
// Get publication titles.
$sql = "SELECT pm.meta_key, pm.meta_value title, pm.post_id researcher
FROM {$wpdb->posts} p
JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
WHERE p.post_status = 'publish'
AND pm.meta_key LIKE 'publication_%_title'
AND pm.meta_value LIKE '%search-term%';";
$titles = $wpdb->get_results($sql, ARRAY_A);
// Get list of post IDs.
$researcher_ids = implode(', ', array_unique(array_column($titles, 'researcher')));
// Get publication years.
$sql = "SELECT REPLACE(pm.meta_key,'_year','_title') AS meta_key, pm.meta_value year, pm.post_id researcher
FROM {$wpdb->posts} p
JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
WHERE p.post_status = 'publish'
AND pm.meta_key LIKE 'publication_list_%_year'
AND pm.post_id IN ($researcher_ids);";
$years_raw = $wpdb->get_results($sql, ARRAY_A);
$years = [];
// Reformat the publication years.
foreach ($years_raw as $year) {
$years[$year['researcher'] . '__' . $year['meta_key']] = $year['year'];
}
// Add the year field to each title.
foreach ($titles as &$title) {
$title['year'] = $years[$title['researcher'] . '__' . $title['meta_key']];
}
// Sort the array by the inner year value.
usort($titles, 'sortByInnerYear');
return $titles;
}
function sortByInnerYear($a, $b): int
{
return $b['year'] <=> $a['year'];
}

Wordpress meta_query

I'm struggling with a wp_query and i need your help.
I have 3 custom fields called "Agenda_day", "Agenda_month", "Agenda_year", representing the day, month and year of an event.
I want to order the results of my query by day, then month, and finally year, ascendingly.
Here is my query :
$query_agenda = new WP_Query(
array(
'posts_per_page' => 8,
'cat' => 4,
'meta_query' =>
array(
'relation' => 'AND',
'day' => array('key' => 'Agenda_day', 'compare' => 'EXISTS'),
'month' => array('key' => 'Agenda_month', 'compare' => 'EXISTS'),
'year' => array('key' => 'Agenda_year', 'compare' => 'EXISTS')
),
'orderby' => array('day' => 'ASC', 'month' => 'ASC', 'year' => 'ASC')
)
);
And this does not work ... can you explain me why and show me how to fix it ?
Thank you !
EDIT
Here is the executed query (results are returned but not well ordered)
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (4) ) AND ( wp_postmeta.meta_key = 'Agenda_jour' AND mt1.meta_key = 'Agenda_mois' AND mt2.meta_key = 'Agenda_annee' ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY CAST(wp_postmeta.meta_value AS CHAR) ASC, CAST(mt1.meta_value AS CHAR) ASC, CAST(mt2.meta_value AS CHAR) ASC LIMIT 0, 8
https://codex.wordpress.org/Class_Reference/WP_Query
https://codex.wordpress.org/Class_Reference/WP_Meta_Query
please check above both links you will get your answer and your orderby arguments which you are passing it's wrong
Problem solved !
i was dealing with different formats in the custom fields : some days and months were on 1 character ("6") and others on two ("06"), so the system couldn't sort it well
second point is, as i'm dealing with dates, i have to reorder the "orderby" instruction from "day, month, year" to "year, month, day"

WP_Query: Filter tax_query OR meta_query

How can I combine the result of the tax_query and meta_query?
ex:
tax_query results to:
Post1, Post2.
and meta_query results to:
Post3.
I want to combine the results of the 2 queries.
Here is my partial code:
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'tax1',
'field' => 'term_id',
'terms' => array(1,2,3),
),
array(
'taxonomy' => 'tax2',
'field' => 'term_id',
'terms' => array(1,2,3),
),
),
'meta_query' => array(
array(
'key' => 'meta1',
'value' => '1',
'compare' => '=',
),
),
The same question was asked here but wasn't answered. So I'm re-opening it.
Thanks!
It sounds like you are wanting it to act like an 'OR' where a post that matches either the tax_query or meta_query is returned.
Unfortunately, that's not possible using WP_Query.
You'll either need to write a custom database call, or just query them separately and do your checking on the PHP end (that's ok if you know you'll have a relatively small number of results, but be warned this won't necessarily scale to a huge site).
The only way to achieve a query like that is creating a custom query to the db using the $wpdb global and SQL statements. I think it would be something like this:
global $wpdb;
$querystr =
"
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy as term_taxonomy1 ON($wpdb->term_relationships.term_taxonomy_id = term_taxonomy1.term_taxonomy_id)
LEFT JOIN $wpdb->term_taxonomy as term_taxonomy2 ON($wpdb->term_relationships.term_taxonomy_id = term_taxonomy2.term_taxonomy_id)
LEFT JOIN $wpdb->terms as tax1_term1 ON(term_taxonomy1.term_id = tax1_term1.term_id)
LEFT JOIN $wpdb->terms as tax1_term2 ON(term_taxonomy1.term_id = tax1_term2.term_id)
LEFT JOIN $wpdb->terms as tax1_term3 ON(term_taxonomy1.term_id = tax1_term3.term_id)
LEFT JOIN $wpdb->terms as tax2_term1 ON(term_taxonomy2.term_id = tax2_term1.term_id)
LEFT JOIN $wpdb->terms as tax2_term2 ON(term_taxonomy2.term_id = tax2_term2.term_id)
LEFT JOIN $wpdb->terms as tax2_term3 ON(term_taxonomy2.term_id = tax2_term3.term_id)
WHERE
(
(
(term_taxonomy1.taxonomy = 'tax1')
AND
(tax1_term1.term_id = 1 AND tax1_term2.term_id = 2 AND tax1_term3.term_id = 3)
)
OR
(
(term_taxonomy2.taxonomy = 'tax2')
AND
(tax2_term1.term_id = 1 AND tax2_term2.term_id = 2 AND tax2_term3.term_id = 3)
)
)
OR
(
$wpdb->postmeta.meta_key = 'meta1' AND $wpdb->postmeta.meta_value = '1'
)
ORDER BY $wpdb->postmeta.meta_value ASC
"
$pageposts = $wpdb->get_results($querystr, OBJECT);
Then you could iterate over $pageposts as with a WP_Query loop.

how to use the query code in drupal?

now i want to get the node title list,and show it on the term page. if i know the term id. how i can get the node title under this term id? thank you. eg:the term id is 2. drupal version is 6
this is the query,
$query = "SELECT n.nid, n.title FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid = arg(2)";
but i don't know how to pager it and output it in list?
A simpler way would be a view, either as a block or page (who could replace the taxonomy term page). If you want code, something like this should work:
<?php
$items = array();
$result = pager_query("SELECT n.nid, n.title FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid = %d", arg(2), 0, 10);
while ($row = db_fetch_object($result)) {
$items[] = l($row->title, 'node/' . $row->nid);
}
print theme('item_list', $items, t('Nodes in this category'));
print theme('pager');
$query = "SELECT n.nid, n.title FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid = %d";
$results = db_query($query,arg(2));
// define table headers(th)
$header = array(
array('data' => t('Node NID')),
array('data' => t('Node Title')),
);
//define rows in table
while($row = db_fetch_array($res)){
$rows[] = array ($row[nid],$row[title]);
}
//leave the theming to hands of Drupal
echo theme('table',$header,$rows);

Resources